【发布时间】:2016-01-04 10:54:46
【问题描述】:
如标题。
我一直在谷歌搜索并阅读threads...
我很确定没有其他地方可以使用 e.Handle 来阻止异常冒泡到 UnhandledException 处理程序。
我测试我的代码如下:
App.xaml.cs
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException +=
CurrentDomain_UnhandledException;
Application.Current.DispatcherUnhandledException +=
Current_DispatcherUnhandledException;
}
private void Current_DispatcherUnhandledException(
object sender,
DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.StackTrace);
e.Handled = true;
}
void CurrentDomain_UnhandledException(
object sender,
UnhandledExceptionEventArgs e)
{
MessageBox.Show(e.ExceptionObject);
}
}
绑定到 DataGrid 的 SelectedItem 的 ViewModel 属性:
private object _selectedItem;
public object SelectedItem
{
get
{
return this._selectedItem;
}
set
{
throw new Exception("Test");
if (this._selectedItem == value)
return;
this._selectedItem = value;
this.RaisePropertyChanged();
}
}
由于某种原因,我在其他任何地方都可以看到正在处理的异常,出现了消息框。但是只有当我在DataGrid中选择时,才触发SelectedItem中的setter,异常只显示在Visual Studio中,而没有被handlers处理......是什么原因?
【问题讨论】:
-
VS 被配置为在某些异常情况下中断。另外,我认为它不知道您在完全不同的地方捕获异常,因此它会将其视为未处理的异常。
标签: wpf mvvm datagrid exception-handling