有时,在不可重现的情况下,我的WPF应用程序崩溃而没有任何消息.应用程序立即关闭.
实现全局Try/Catch块的最佳位置在哪里.至少我必须实现一个消息框:"抱歉给您带来不便......"
我正在开发一个应用程序,它使用.NET 3.5 sp1和C#从Web加载位图.
加载代码如下:
try {
CurrentImage = pics[unChosenPics[index]];
bi = new BitmapImage(CurrentImage.URI);
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.DownloadCompleted += new EventHandler(bi_DownloadCompleted);
AssessmentImage.Source = bi;
}
catch {
System.Console.WriteLine("Something broke during the read!");
}
Run Code Online (Sandbox Code Playgroud)
并且在bi_DownloadCompleted上加载的代码是:
void bi_DownloadCompleted(object sender, EventArgs e) {
try {
double dpi = 96;
int width = bi.PixelWidth;
int height = bi.PixelHeight;
int stride = width * 4; // 4 bytes per pixel
byte[] pixelData = new byte[stride * height];
bi.CopyPixels(pixelData, stride, 0);
BitmapSource …Run Code Online (Sandbox Code Playgroud) 我正试图捕获我的WPF应用程序中的所有异常.我尝试了以下代码,但它不工作我不知道为什么?
<Application x:Class="DBFilter.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Exit="Application_Exit"
DispatcherUnhandledException ="AppDispatcherUnhandledException"
>
<Application.Resources>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(AppDomainUnhandledExceptionHandler);
System.Windows.Forms.Application.ThreadException += new
ThreadExceptionEventHandler(Application_ThreadException);
Application.Current.DispatcherUnhandledException += new
DispatcherUnhandledExceptionEventHandler(AppDispatcherUnhandledException);
}
void AppDomainUnhandledExceptionHandler(object sender,
UnhandledExceptionEventArgs ea)
{
Exception ex = (Exception)ea.ExceptionObject;
MessageBox.Show(ex.Exception.InnerException.Message);
}
void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.InnerException.Message);
}
void AppDispatcherUnhandledException(object
sender,DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.InnerException.Message);
}
Run Code Online (Sandbox Code Playgroud)
稍后,我将把所有异常写入日志表.