MVVM Exception Handling

后端 未结 3 1160
我寻月下人不归
我寻月下人不归 2021-02-04 14:22

I have a WPF Application that I have been trying to write in the MVVM style. If an Exception is thrown (like when a document is opened), I would like to display a MessageBox.

3条回答
  •  你的背包
    2021-02-04 15:19

    Use a service:

    public void SomeMethodInYourViewModel()
    {
        try
        {
            DoSomethingDangerous();
        }
        catch (Exception ex)
        {
            ServiceLocator.Resolve().ShowMessage(ex.Message);
        }
    }
    

    You have now decoupled your VMs from the presentation of messages. You may even decide not to use the standard (ugly) message boxes at all and that won't affect your VMs.

提交回复
热议问题