在XamlParseException中捕获丢失的DLL [英] Catching missing DLL in XamlParseException

查看:80
本文介绍了在XamlParseException中捕获丢失的DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常不寻常的问题,即如果客户端计算机上缺少DLL,则应用程序将冻结并显示标准应用程序无响应 。但是,据我所知,我想找到一种方法来捕获此异常(缺少DLL),并在对话框中显示消息,显示有意义的信息以帮助识别缺少的DLL。


在客户端计算机上 debugging 之后,我将收到错误消息:


PresentationFramework.dll中出现类型为'System.Windows.Markup.XamlParseException'的第一次机会异常


其他信息:无法加载文件或程序集'Some.DLL'或其依赖项之一。系统找不到指定的文件。


版本中,应用程序崩溃并且没有响应。


查看文档对于此错误,通常在 InitializeComponent(); 内部会出现 XamlParseException 方法:


对于应用程序的页面,当抛出XamlParseException时,通常是在您的页面类进行的InitializeComponent调用的上下文中,即WPF应用程序模型在每页级别使用WPF XAML解析器的入口点。 因此,另一种可能的处理策略是将try / catch块放置在InitializeComponent中。但是,此技术不能与模板,视觉设计图面以及其他生成的与InitializeComponent挂钩的源集成在一起。


因此,我可以做类似的事情:

  public MyView()
{
try
{
InitializeComponent();
}
catch(XamlParseException ex)
{
//对错误进行一些有用的处理。
}
}

当然可以,但是实际上需要使用此代码控制,这显然是荒谬的。更不用说它并不能真正解决缺少DLL的问题。


因此,我的问题是:



  • 是否可以捕获丢失的DLL并显示包含该DLL名称的消息?

  • 还有一种更优雅的方式来捕获 XamlParseException


谢谢。

解决方案

是的,当然有可能。



为此,您将需要覆盖应用程序首次启动时发生的事情。 / p>

打开您的 Application.xaml.vb ,并添加以下代码:

 受保护的重写void OnStartup(StartupEventArgs e)
{
//为UnhandledException事件
AppDomain.CurrentDomain添加事件处理程序。 UnhandledException + =新的UnhandledExceptionEventHandler(HandleException);
//启动应用程序
base.OnStartup(e);
}
//引发异常时的处理方法
void HandleException(object sender,UnhandledExceptionEventArgs e)
{
//做一些异常
MessageBox.Show(e.ExceptionObject.ToString());
}

e.ExceptionObject.ToString( )包含此问题。在您描述的情况下,可能会有嵌套的异常,内部的异常说明: System.IO.FileNotFoundException:无法加载文件或程序集 {在此处缺少DLL名称}或其依赖项之一。该系统找不到指定的文件。在{Project}。{引发错误的地方}


I have quite an unusual problem where if there is a missing DLL on the client machine, the application will freeze and display the standard "The application is not responding". However, as I know what the issue is, I'd like to find a way to catch this exception (Missing DLL) and display the message in a dialog displaying meaningful information to help identify which DLL is missing. This will allow the application to have a more graceful death.

Upon debugging on the client machine, I receive the error:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Could not load file or assembly 'Some.DLL' or one of its dependencies. The system cannot find the file specified.

In release however, the application crashes and is not responding.

Looking at the documentation for this error, a XamlParseException usually occurs inside the InitializeComponent(); method:

For pages of an application, when the XamlParseException is thrown, it is usually in the context of the InitializeComponent call made by your page class, which is the entry point for the WPF application model's usage of the WPF XAML parser at the per-page level. Therefore another possible handling strategy is to place try/catch blocks in InitializeComponent. However, this technique does not integrate well with templates, visual design surfaces and other generated sources that hook up InitializeComponent.

So, I can do something like this:

public MyView()
{
    try
    {  
        InitializeComponent();
    }
    catch (XamlParseException ex)
    { 
        //Do something useful with the error.
    }
}

This is certainly possible, however it would require using this code in practically all controls, which is obviously ridiculous. Not to mention that it doesn't really solve the issue of a missing DLL.

So, my questions are:

  • Is it possible to trap a missing DLL and display a message containing the name of said DLL?
  • Is there a more elegant way of catching a XamlParseException?

Thanks.

解决方案

Yes, it is certainly possible.

To do this, you will need to override what happens when the application first starts up.

Open your Application.xaml.vb, and add the following code:

    protected override void OnStartup(StartupEventArgs e)
    {
        // add an event handler for the UnhandledException event
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleException);
        // start up the application
        base.OnStartup(e);
    }
    // what to do when the exception is thrown
    void HandleException(object sender, UnhandledExceptionEventArgs e)
    {
        // do something with the exception
        MessageBox.Show(e.ExceptionObject.ToString());
    }

The output from the e.ExceptionObject.ToString() contains the problem. In the case you describe, there will probably be nested exceptions, the inner one stating : System.IO.FileNotFoundException: Could not load file or assembly '{missing DLL name here}' or one of its dependencies. The system cannot find the file specified. at {Project}.{where the error was thrown}

这篇关于在XamlParseException中捕获丢失的DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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