C# 如何在设计MVVM模式应用程序中创建MessageBox或Dialog Box
对于那些MVVM纯粹主义者,我的问题是,对于“如何在MVVM设计模式应用程序中创建消息框或对话框”这一问题,是否有一个更简单、用户可读且可单元测试的代码解决方案,那么我在这里想到了什么?免责声明,我不是MVVM纯粹主义者,如果这意味着更简单、用户可读和单元可测试的代码,我将在视图的代码后面添加几行代码。我的解决方案建立在什么基础上。解决方案中需要注意的第一件事是,在处理MessageBox的过程中,有View的代码。从这一点上,我意识到在视图的代码隐藏中添加代码已经走上了一条非MVVM纯粹主义的道路。因此,我的解决方案充分利用了这个单一的规则破坏,而没有额外的规则破坏 BaseModel.csC# 如何在设计MVVM模式应用程序中创建MessageBox或Dialog Box,c#,wpf,mvvm,parent-child,dialog,C#,Wpf,Mvvm,Parent Child,Dialog,对于那些MVVM纯粹主义者,我的问题是,对于“如何在MVVM设计模式应用程序中创建消息框或对话框”这一问题,是否有一个更简单、用户可读且可单元测试的代码解决方案,那么我在这里想到了什么?免责声明,我不是MVVM纯粹主义者,如果这意味着更简单、用户可读和单元可测试的代码,我将在视图的代码后面添加几行代码。我的解决方案建立在什么基础上。解决方案中需要注意的第一件事是,在处理MessageBox的过程中,有View的代码。从这一点上,我意识到在视图的代码隐藏中添加代码已经走上了一条非MVVM纯粹主义的
public class BaseModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
}
public class MessageBoxModel : BaseModel
{
private string msgboxcontent;
public string MsgBoxContent
{
get
{
return msgboxcontent;
}
set
{
this.msgboxcontent = value;
OnPropertyChanged("MsgBoxContent");
}
}
}
MessageBoxModel.cs
public class BaseModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
}
public class MessageBoxModel : BaseModel
{
private string msgboxcontent;
public string MsgBoxContent
{
get
{
return msgboxcontent;
}
set
{
this.msgboxcontent = value;
OnPropertyChanged("MsgBoxContent");
}
}
}
MessageBoxViewModel.cs//子视图模型
public class MessageBoxViewModel
{
private MessageBoxModel MB;
public MessageBoxViewModel()
{
MB = new MessageBoxModel();
MB.msgboxcontent = "My Message Box Content";
}
public MessageBoxModel MessageBoxModel
{
get
{
return MB;
}
}
public class MainWindowViewModel
{
private MessageBoxViewModel child_MsgBoxviewmodel;
public MainWindowViewModel()
{
child_MsgBoxviewmodel = new MessageBoxViewModel();
}
public MessageBoxViewModel MsgBoxViewModel
{
get
{
return child_MsgBoxviewmodel;
}
}
}
MainWindowViewModel.cs//父视图模型
public class MessageBoxViewModel
{
private MessageBoxModel MB;
public MessageBoxViewModel()
{
MB = new MessageBoxModel();
MB.msgboxcontent = "My Message Box Content";
}
public MessageBoxModel MessageBoxModel
{
get
{
return MB;
}
}
public class MainWindowViewModel
{
private MessageBoxViewModel child_MsgBoxviewmodel;
public MainWindowViewModel()
{
child_MsgBoxviewmodel = new MessageBoxViewModel();
}
public MessageBoxViewModel MsgBoxViewModel
{
get
{
return child_MsgBoxviewmodel;
}
}
}
MainWindow.xaml.cs//父视图
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
private void MessageBoxButton_Clicked(object sender, RoutedEventArgs e)
{
// Instantiate the dialog box
MessageBoxView dlgView = new MessageBoxView();
// Call parent view model to get child view model
MainWindowViewModel mvm = this.DataContext as MainWindowViewModel;
// Configure the dialog box
dlgView.DataContext = mvm.MsgBoxViewModel ;
// Open the dialog/message box
dlgView.ShowDialog();
}
}
MessageBoxView.xaml.cs//子视图
public partial class MessageBoxView : Window
{
public MessageBoxView()
{ //DialogBox
InitializeComponent();
}
}
这里不显示xmal文件,因为这是使用MVVM的一个优点。视图样式完全由UI设计器决定
一旦有人单击MessageBox按钮,消息框就会出现
然后,可以像往常一样对模型和viewmodel类进行单元测试,而不必担心测试期间的弹出窗口
首先让我说,我也不是一个MVVM纯粹主义者,但我已经发现了一件事,从我的角度来看,我会在您的代码中进行更改
- 您在那里的按钮单击事件应更改为xaml中与MainWindowViewModel的ICommand的绑定