注意:这最终是一个相当长的答案 - 如果有什么不清楚的地方请问我
对话窗口的实现在 MVVM 设计中是一个有争议的问题,不同的人使用不同的方法。
和你一样,我决定不使用任何框架并手动实现大多数东西。当谈到对话窗口时,我选择通过从 ViewModel 内部启动对话窗口来务实地实现 MVVM。此外,我允许每个 Dialog ViewModel 引用它所显示的 Window,因此它可以在适当的时候关闭它(详情如下)。这打破了一些严格的 MVVM “规则”,但它完成了工作。
这样做的主要缺点是,如果您正在测试通过对话框的内容,它可能会破坏单元测试。但是,您可以走很长一段路而不会遇到这个问题,而且它还没有困扰我。
我已经建立了一些对话框 ViewModel 库,我可以轻松地对其进行扩展。这里的代码太多了,但我会向您展示重点。
对话框的基本视图模型
我的每个对话窗口都有一个继承自 DialogViewModelBase 的 ViewModel,这与我的常规 ViewModelBase 类似,因为它提供对 INotifyPropertyChanged 等的支持。有趣的部分是这个公共方法,我从它调用在哪里启动对话框:
/// <summary>
/// Creates window instance for this dialog viewmodel and displays it, getting the dialog result.
/// </summary>
public void ShowDialogWindow()
{
// This is a property of the DialogViewModelBase class - thus, each DialogViewModel holds a reference to its own DialogWindow:
this.DialogWindow = new Dialogs.Views.DialogWindow();
// Tell the DialogWindow to display this ViewModel:
this.DialogWindow.DataContext = this;
// Launch the Window, using a method of the Window baseclass, that only returns when the window is closed:
this.DialogWindow.ShowDialog();
}
通过上述方法启动的窗口将在其Window.DialogResult 属性设置后关闭。这就是为什么DialogWindow 是DialogViewModelBase 类的属性 - 当子类化对话框ViewModel 想要关闭对话框窗口时,它只是设置结果:
protected void CloseDialogWithResult(bool dialogWindowResult)
{
// Setting this property automatically closes the dialog window:
this.DialogWindow.DialogResult = dialogWindowResult;
}
对话框视图的宿主窗口
ShowDialogWindow 方法实例化的Dialogs.Views.DialogWindow 类在 XAML 中定义,是Window 的子类。它有两个重要的特点。首先是它的主要内容元素只是一个绑定到当前上下文的ContentControl。这允许我为DialogViewModelBase 的不同子类定义不同的Views,并且DialogWindow 将根据上下文的类型托管相应的View:
<ContentControl Content="{Binding}" /> <!-- In reality this is inside a border etc but its simplified here for demonstration -->
DialogWindow XAML 的第二个重要特性是它定义了哪个对话框 Views 与哪个对话框 ViewModels 一起使用。这是一个示例:
<Window.Resources>
<!-- DEFAULT ViewModel-View TEMPLATES -->
<DataTemplate DataType="{x:Type dialogs:YesNoMessageBoxDialogViewModel}">
<views:MessageBoxView />
</DataTemplate>
<DataTemplate DataType="{x:Type dialogs:ErrorDialogViewModel}">
<views:ErrorDialogView/>
</DataTemplate>
</Window.Resources>
这一切的作用是,我可以将对话框定义为DialogViewModelBase 的子类,并为每个对话框实现一个View,然后告诉DialogWindow 哪个View 其ContentControl 必须显示哪个对话框@987654348 @。
启动对话框并获得结果
下面是我的一个应用程序ViewModels 的示例,我在其中启动了一个对话框窗口,允许用户选择要创建的资产类型:
public void CreateNewAsset()
{
// Instantiate desired Dialog ViewModel:
Dialogs.NewAssetTypeSelectionDialogViewModel dialog = new Dialogs.NewAssetTypeSelectionDialogViewModel();
// Launch Dialog by calling method on Dialog base class:
dialog.ShowDialogWindow();
// Execution will halt here until the Dialog window closes...
// The user's selection is stored in a property on the dialog ViewModel, and can now be retrieved:
CalculatorBase.AssetTypeEnum newAssetType = dialog.AssetType;
switch (newAssetType)
{
// Do stuff based on user's selection...
}
}
PS:我真的应该写一篇关于这个的博客文章——当我这样做时,我会在这里发布链接,因为博客文章可能会有更完整的代码示例。