如何在 MVVM 中显示消息框 [英] how to show Messagebox in MVVM

查看:24
本文介绍了如何在 MVVM 中显示消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
你是如何成功实现 MessageBox.Show() 功能的?MVVM?

我想在我的 MVVM WPF 应用程序中显示消息框.所以从哪里调用 MessageBox.Show().

I want to show message box in my MVVM WPF application. so from where to call MessageBox.Show().

推荐答案

在 Windows 窗体或没有 MVVM 的 WPF 中,您只需说 MessageBox.Show() 就可以了!如果你能在 MVVM 中做同样的事情不是很好吗?

In Windows Forms, or WPF without MVVM, you could just say MessageBox.Show() and that was it! Wouldn't it be nice if you could do the same in MVVM?

这里有一种方法可以做到这一点 - 它尽可能接近 MessageBox.Show().

Here is one way to do this - and it is as close as I can get to MessageBox.Show().

这是 MVVM 友好的 MessageBox_Show()

Here is the MVVM friendly MessageBox_Show()!

public class MyViewModel: ViewModelBase
{
    protected void AskTheQuestion()
    {
        MessageBox_Show(ProcessTheAnswer, "Are you sure you want to do this?", "Alert", System.Windows.MessageBoxButton.YesNo);
    }

    public void ProcessTheAnswer(MessageBoxResult result)
    {
        if (result == MessageBoxResult.Yes)
        {
            // Do something
        }
    }
}

多田!

这是它的工作原理:

MessageBox_Show 实际上所做的只是触发一个事件,因此它非常适合 MVVM.ViewModel 对任何可能使用或不使用它的视图一无所知,并且它不会自行执行 Windows MessageBox 的显示,因此它也可以安全地进行单元测试.

All that MessageBox_Show actually does is fire an event, so it is perfectly MVVM friendly. The ViewModel knows nothing about any view that may or may not be consuming it, and it doesn't perform the showing of a Windows MessageBox on it's own, so it can also be safely unit tested.

要在视图中使用它,这将导致它实际显示一个 MessageBox,您只需订阅该事件并在事件处理程序中调用 e.Show(),如下所示:

To use it in a View, which will cause it to actually show a MessageBox, you just subscribe to the event and call e.Show() in the event handler, like this:

public partial class MyView : UserControl
{
    public MyView()
    {
        InitializeComponent();

        this.DataContext = new MyViewModel();
        (this.DataContext as MyViewModel).MessageBoxRequest += new EventHandler<MvvmMessageBoxEventArgs>(MyView_MessageBoxRequest);
    }

    void MyView_MessageBoxRequest(object sender, MvvmMessageBoxEventArgs e)
    {
        e.Show();
    }
}

这就是显示 MVVM 友好的 Windows MessageBoxes 所需要做的全部.

And that is all you need to do to show MVVM friendly Windows MessageBoxes.

下面的代码只需要在你的项目中实现一次,或者你可以把它放在一个可重用的共享库中.

The code below only needs to be implemented once in your project, or you can put it in a reusable shared library.

将此添加到您的 ViewModel 基类,以便它可以在任何 ViewModel 中使用:

Add this to your ViewModel base class so it can be used from any ViewModel:

public class ViewModelBase : INotifyPropertyChanged
{
    //...

    public event EventHandler<MvvmMessageBoxEventArgs> MessageBoxRequest;
    protected void MessageBox_Show(Action<MessageBoxResult> resultAction, string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxOptions options = MessageBoxOptions.None)
    {
        if (this.MessageBoxRequest != null)
        {
            this.MessageBoxRequest(this, new MvvmMessageBoxEventArgs(resultAction, messageBoxText, caption, button, icon, defaultResult, options));
        }
    }
}

然后为事件处理程序添加 EventArgs 类:

And then add the EventArgs class for the event handler:

public class MvvmMessageBoxEventArgs : EventArgs
{
    public MvvmMessageBoxEventArgs(Action<MessageBoxResult> resultAction, string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxOptions options = MessageBoxOptions.None)
    {
        this.resultAction = resultAction;
        this.messageBoxText = messageBoxText;
        this.caption = caption;
        this.button = button;
        this.icon = icon;
        this.defaultResult = defaultResult;
        this.options = options;
    }

    Action<MessageBoxResult> resultAction;
    string messageBoxText;
    string caption;
    MessageBoxButton button;
    MessageBoxImage icon;
    MessageBoxResult defaultResult;
    MessageBoxOptions options;

    public void Show(Window owner)
    {
        MessageBoxResult messageBoxResult = MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options);
        if (resultAction != null)resultAction(messageBoxResult);
    }

    public void Show()
    {
        MessageBoxResult messageBoxResult = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options);
        if (resultAction != null) resultAction(messageBoxResult);
    }
}

单元测试很简单:

target.AskTheQuestion();
target.ProcessTheAnswer(System.Windows.MessageBoxResult.Yes);

快乐编码!

这篇关于如何在 MVVM 中显示消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆