[英]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)。 只需在代碼中添加對程序集的引用,就可以了。
代碼中的任何引用都可以,但是我喜歡這一點。
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.