我在我的項目中引用了一個dll。當我啓動我的WPF應用程序並且dll不存在於同一文件夾中時,我在Visual Studio中得到未處理的XamlParseException
。 當我在發佈模式下運行時,應用程序崩潰。XAMLParseException在啓動時找不到引用的dll
我嘗試在應用程序啓動之前使用下面的代碼處理該異常。 不幸的是異常的消息隻字未提未找到該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文件 – tzippy
OK,所以它並沒有在所有幫助......所以請告訴我,它只在發佈模式下而不在調試模式下崩潰?你是否嘗試從Visual Studio的CTRL + F5鍵釋放模式,或者你是否創建了一些設置並從那裏啓動它?也許安裝程序不會複製DLL?我和其他回覆者一樣困惑...... –