繁体   English   中英

XamlParseException当启动时找不到引用的dll

[英]XamlParseException when referenced dll not found on startup

我在项目中引用了一个dll。 当我启动WPF应用程序并且该dll不存在于同一文件夹中时,我在Visual Studio中收到未处理的XamlParseException 当我在发布模式下运行时,应用程序崩溃了。

我尝试使用以下代码在App启动之前处理该异常。 不幸的是,异常消息没有说明未找到的dll,但有以下消息:

Cannot create instance of 'MainWindow' defined in assembly 'App.Demo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation.  Error in markup file 'MainWindow.xaml' Line 1 Position 9.

但是内部异常具有以下内容:

InnerException: System.Reflection.TargetInvocationException
       Message=Exception has been thrown by the target of an invocation.
       Source=mscorlib
       StackTrace:
            at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
            at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
            at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
            at System.Activator.CreateInstance(Type type, Boolean nonPublic)
            at System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail)
       InnerException: System.IO.FileNotFoundException
            Message=Could not load file or assembly 'MyLibrary, Version=1.0.9999.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
            Source=App.Demo
            FileName=MyLibrary, Version=1.0.9999.0, Culture=neutral, PublicKeyToken=null
            FusionLog==== Pre-bind state information ===

在找不到引用的库的情况下,是否有一种通用的方法来处理这些情况?

public partial class App : Application
    {

        protected override void OnStartup(StartupEventArgs e)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);

        }

        void UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception) e.ExceptionObject;
            System.Windows.MessageBox.Show(ex.Message);
            Application.Current.Shutdown();
        }


    }

还奇怪的是:尽管我调用Application.Current.Shutdown ,但此后再次引发了异常,导致我的应用程序崩溃。

编辑:添加了MainWindow.xaml和App.xaml的代码

App.xaml:

<Application x:Class="Demo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

MainWindow.xaml:

<Window x:Class="Demo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="My Application" Height="768" Width="1024" MaxWidth="1024" MinWidth="1024" MinHeight="768" MaxHeight="768" Background="#004B93">

问题在于XAML解析器实际上不会引起对引用程序集的引用,因此在构建时就删除了引用程序集。 构建链这样做是为了防止复制未使用的程序集(例如,默认引用的System.Core)。 只需在代码中添加对程序集的引用,就可以了。

https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/7f255570-dd53-41f8-b8c4-a160ba325c90/reference-not-loaded-into-assembly-when-only-using-xaml-for-被引用的代码错误?forum = wpf

代码中的任何引用都可以,但是我喜欢这一点。

using System;

namespace MyReferencedAssembly
{
    /// <summary>
    /// Use to force an assembly reference
    /// </summary>
    /// <seealso cref="System.Attribute" />
    [AttributeUsage(AttributeTargets.Assembly)]
    public class AssemblyReferenceAttribute : Attribute
    {
    }
}

在应用程序的AssemblyInfo.cs中,只需添加一个引用属性:

[assembly: MyReferencedAssembly.AssemblyReference]

我不确定这一点,但我认为我们需要查看App.xaml和MainWindow.xaml才能解决问题。 也许您正在使用DLL中定义的资源类型在这两种方式中创建静态资源。 xaml解析器找不到程序集,并且无法创建此对象的实例。 您是否同时使用StartupUri和Startup?

当我启动WPF应用程序并且该dll不存在于同一文件夹中时

您引用的dll必须可由您的应用程序访问-默认情况下,它将与可执行文件位于同一文件夹中。 如果找不到dll,它将如何从库中加载您使用的东西?

在Visual Studio中,在引用的属性下,确保将“复制本地”设置为true。 然后,为了良好运行,运行Build-> Clean Solution,然后运行Build-> Rebuild Solution。 MyLibrary.dll现在应该在发布文件夹中,并且您不应收到异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM