ViewModel中的Silverlight异常没有在Application_UnhandledException中被捕获 [英] Silverlight exception in ViewModel not caught in Application_UnhandledException

查看:168
本文介绍了ViewModel中的Silverlight异常没有在Application_UnhandledException中被捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况,其中ViewModel中抛出的异常不会冒泡到App.xaml.c中的Application_UnhandledException。



我在ViewModel中有一个ObservableCollection绑定到ComboBox中的ItemSourceProperty。 ComboBox的SelectedItemProperty绑定到ViewModel中的一个属性。



当用户在ComboBox中选择一个条目时,该属性在ViewModel中正确调用。执行一点逻辑,并在ViewModel中设置另一个属性(称为property2)。但是,property2中存在未处理的异常。这个例外只是消失 - 它不会在UI线程上引发。



任何建议,如何一般地解决这个问题或一种方法来捕获任何线程的异常?



请注意,我们有一个自定义的MVVM框架。起初,我认为这是我们框架的一个问题。经过好几个小时的调试,我决定下载Prism4(http://www.microsoft.com/download/en/confirmation.aspx?id=4922),看看是否可以在StockTrader参考应用程序中复制类似的情况。 p>

我可以重现完全相同的情景!我很乐意提供有关如何在Prism4中设置异常的详细信息。



非常感谢任何帮助或指针,一般方法来捕获Silverlight中的所有未处理的异常



Regards,
Travis

解决方案

运行时允许您使用异常进行验证,运行时的get-value-for-binding操作位于一个大的try-catch块中。



在ILSpy中查看System.Windows.Data.BindingExpression.UpdateValue()的详细信息(在System.Windows中)WPF版本可能更容易理解(UpdateSource) )。



我不认为可以自定义运行时的行为来重新抛出你自己的异常。你可以从代码中看到它反驳了一些关键的。

  OutOfMemoryException,StackOverflowException,AccessViolationException,ThreadAbortException 

由于其他异常不被重新抛出,实际上它们已被处理。



我认为您的解决方案是捕获跟踪,或者在属性设置器中有自己的异常处理。


I have the following situation where an Exception thrown in a ViewModel does not bubble up to the Application_UnhandledException in App.xaml.cs.

I have an ObservableCollection in the ViewModel bound to the ItemSourceProperty in a ComboBox. The SelectedItemProperty of the ComboBox is bound to a property in the ViewModel.

When the users selects an entry in the ComboBox the property is correctly called in the ViewModel. A bit of logic is performed and another property (call it property2) is set in the ViewModel. However, there is an unhandled exception in property2. The exception just "disappears" - it does not get raised on the UI thread.

Any suggestions for how to generally fix this issue or an approach to catch exceptions on any thread?

Note that we have a custom built MVVM framework. At first, I thought it was an issue with our framework. After many hours debugging, I decided to download Prism4 (http://www.microsoft.com/download/en/confirmation.aspx?id=4922) and see if a similar scenario could be reproduced in the StockTrader reference application.

I can reproduce the exact same scenario! I would be happy to provide details on how to set up the exception in Prism4.

Any help or pointers on a general approach to catch ALL unhandled exceptions in Silverlight is greatly appreciated.

Regards, Travis

解决方案

Since the runtime allows you to use exceptions for validation purposes, the runtime's get-value-for-binding operation is in one big try-catch block.

Take a look at System.Windows.Data.BindingExpression.UpdateValue() in ILSpy for details (in System.Windows. WPF version may be easier to understand (UpdateSource)).

I don't think its possible to customize the behavior of the runtime to rethrow your own exceptions. You can see from the code that it does rethrow some critical ones.

   OutOfMemoryException, StackOverflowException, AccessViolationException, ThreadAbortException

Since other exceptions are not rethrown, they are, in fact, handled.

I think your solution is to either capture the trace, or have your own exception handling in property setters.

这篇关于ViewModel中的Silverlight异常没有在Application_UnhandledException中被捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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