我在哪里可以捕获MVVM中的异常? [英] Where do I catch Exceptions in MVVM?

查看:355
本文介绍了我在哪里可以捕获MVVM中的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图模型类有一个方法(不知道这是否是良好做法,或者视图模型是否应该是严格的属性和属性更改机制),它连接到一个服务。当然,我想在连接或断开连接时处理任何可能的WCF异常。

My view model class has a method (not sure if that is good practice or if view models are supposed to be strictly property and property changing mechanisms) that connects to a service. Of course I want to handle any possible WCF exceptions when connecting or disconnecting.

让我们使用未找到的端点,因为这是一个例外,我想要用户的关注。考虑粗略的代码示例:

Let's use endpoint not found as an example considering that is an exception that I would want to bring to the user's attention. Consider the rough code example:

public void Connect()
{
    ServiceClient proxy = null;
    try
    {
        proxy = new ServiceClient();
        proxy.Subscribe();
        // ...
    }
    catch(EndpointNotFoundException)
    {
        // should I do something here?
    }
    // .. other WCF related exception catches and a finally
}

是否可以直接在catch中调用System.Windows.MessageBox.Show()被认为是好的做法,或者是否应该重新启动异常,这样我的WPF应用程序的另一层会捕获它?如果是这样,那么在哪里可以找到这样的例外?

Is it considered good practice to maybe invoke System.Windows.MessageBox.Show() directly within the catch or should I maybe rethrow the exception so another layer of my WPF application catches it? If so, where is the ideal place to catch such an exception?

推荐答案

我已经在我的MVVM客户端中处理异常捕获它们并将它们包装在任何 ViewModel ErrorViewModel 属性中,捕获异常。

I've been handling exceptions in my MVVM client by catching them and wrapping them in an ErrorViewModel property of whatever ViewModel caught the exception.

我们假设一个ViewModel A 捕获EndpointNotFoundException。为了提出这个错误,我将异常包装在一个ErrorViewModel中,并将其分配给 A 的Error属性。

Let's say a ViewModel A catches the EndpointNotFoundException. To present this error, I wrap the Exception in an ErrorViewModel and assign that to A's Error property.

A 相关联的视图包含一个 ContentControl 绑定到 A 的错误属性。同时,我使用 DataTemplate 将错误视图与ErrorViewModel相关联。在该视图中,可见性 A 的Error属性是否包含异常确定。

The View associated with A contains a ContentControl bound to A's Error property. Meanwhile, I use a DataTemplate to associate an Error View to the ErrorViewModel. In that View, Visibility is determined by whether or not A's Error property contains an exception.

所以 A 的视图包含一个错误消息仅在捕获异常时才会显示的视图,并且可以被用户忽略(错误消息View上的OK按钮调用 A 上的一个命令,该命令清除 A 的Error属性,从而将错误消息视图的可见性更改为 Collapsed )。

So A's View contains an error-message View that will only appear when an exception is caught, and can be dismissed by the user (an OK button on the error-message View invokes a command on A that clears A's Error property, thereby changing the error-message View's visibility to Collapsed).

到目前为止,这似乎是一个很好的方法来保留适当的MVVM解耦。

Thus far, this seems to be a good approach that preserves proper MVVM decoupling.

希望有所帮助。有一种方式,老实说,我将WPF应用程序中的 System.Windows.MessageBox.Show()视为最后的手段。为什么要放弃对UI的丰富控制,以支持那个老东西?说到这里,另一个弹出窗口实现方法

Hope that helps. One way or another, honestly, I'd consider System.Windows.MessageBox.Show() in a WPF app as purely a last resort. Why give up rich control over the UI in favor of that old thing? Speaking of which, here's another popup-implementation approach.

这篇关于我在哪里可以捕获MVVM中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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