如何在设计MVVM模式应用程序中创建MessageBox或对话框

时间:2014-07-19 19:52:46

标签: c# wpf mvvm parent-child dialog

对于那些MVVM Purist,我的问题是,是否有一个更简单,用户可读,可单元测试的问题代码解决方案"如何在MVVM设计模式应用程序中创建消息框或对话框&#34 ;那我在这里想出什么?免责声明,我不是MVVM Purist,如果它意味着更简单,用户可读和可单元测试的代码,我将在View的代码隐藏中添加几行代码。我的解决方案建立在awardcoder.blogspot suggested之上。解决方案中首先注意到的是View的后台处理MessageBox的代码隐藏。从这一点开始,我意识到在View的代码隐藏中添加代码已经走向了一个非MVVM Purist路径。因此,我的解决方案充分利用了这一单一规则的破坏而没有额外的规则破坏。

BaseModel.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);
        }
    }
}

MessageBoxModel.cs

 public class MessageBoxModel : BaseModel
{
    private string msgboxcontent;

    public string MsgBoxContent
    {
        get
        {
            return msgboxcontent;
        }
        set
        {
            this.msgboxcontent = value;
            OnPropertyChanged("MsgBoxContent");
        }
    }
}

MessageBoxViewModel.cs // Child View-Model

public class MessageBoxViewModel
{
    private MessageBoxModel MB;


    public MessageBoxViewModel()
    {
        MB = new MessageBoxModel();
        MB.msgboxcontent = "My Message Box Content";
    }

    public MessageBoxModel MessageBoxModel
    {
        get
        {
            return MB;
        }
    }

MainWindowViewModel.cs //父视图模型

 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设计师。

一旦有人点击了消息框按钮,就会出现消息框。

然后可以像往常一样在模型和视图模型类上进行单元测试,而不必担心测试期间的弹出窗口。

JP

1 个答案:

答案 0 :(得分:0)

首先我要说的是,我也不是MVVM Purist,但我已经发现了一件事,从我的观点来看,我会改变你的代码。

  • 您在那里的按钮单击事件应该更改为xaml中的绑定到您MainWindowViewModel的ICommand。

这将允许您完全删除代码背后的代码,将来您也可以使用此方法应用合同(接口),以使您的解决方案更具可扩展性:)

干杯!