我在我的项目中引用了一个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">
答案 0 :(得分:0)
我不确定这一点,但我认为我们需要看到App.xaml和MainWindow.xaml才能解决问题。也许您正在使用DLL中定义的资源类型在其中任何一个中创建静态资源。 xaml解析器无法找到程序集,也无法创建此对象的实例。 你也在同时使用StartupUri和Startup吗?
答案 1 :(得分:0)
当我启动WPF应用程序并且dll不在同一文件夹中时
您引用的dll需要可由应用程序访问 - 默认情况下,它将与可执行文件位于同一文件夹中。如果它找不到dll,它怎么能加载你从库中使用的东西?
在Visual Studio中,在引用的属性下,确保将“Copy Local”设置为true。然后,为了好的措施运行Build - &gt;清洁解决方案,然后构建 - &gt;重建解决方案。 MyLibrary.dll现在应该在release文件夹中,你不应该得到例外。
答案 2 :(得分:0)
问题是XAML解析器实际上并没有引用引用的程序集,因此在构建时会删除引用的程序集。构建链执行此操作以防止未使用的程序集被复制(例如,默认情况下引用的System.Core)。只需在代码中添加对程序集的引用,就可以了。
代码中的任何引用都可以,但我喜欢这个。
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]