当我尝试在属性的 Set 子句中运行函数时,我的全局异常处理程序永远不会捕获任何可能出现的异常。我不明白为什么会这样。这是我的代码(3部分)
主窗口.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel_();
}
}
public class ViewModel_ : INotifyPropertyChanged
{
public ViewModel_()
{
}
public string Texting
{
get { return _Texting; }
set
{
_Texting = value;
OnPropertyChanged("Texting");
throw new Exception("BAM!");
}
}
private string _Texting;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
主窗口.xaml
<Window x:Class="TestExceptionHandling.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Text="{Binding Path=Texting,
UpdateSourceTrigger=PropertyChanged}" />
</Grid>
App.xaml.cs(全局异常处理程序所在的位置)
public partial class App : Application
{
public App()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show("SOMETHING IS WRONG!");
}
}