2016-07-21 30 views
0

我在我的項目中引用了一個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"> 

回答

0

我我不知道這一點,但我認爲我們需要看到App.xaml和MainWindow.xaml得到什麼錯誤。也許你正在這些中創建一個靜態資源,並在DLL中定義資源類型。 xaml解析器無法找到該程序集,也無法創建此對象的實例。 你是否也同時使用StartupUri和Startup?

+0

我加入這兩個XAML文件 – tzippy

+0

OK,所以它並沒有在所有幫助......所以請告訴我,它只在發佈模式下而不在調試模式下崩潰?你是否嘗試從Visual Studio的CTRL + F5鍵釋放模式,或者你是否創建了一些設置並從那裏啓動它?也許安裝程序不會複製DLL?我和其他回覆者一樣困惑...... –

0

當我開始我的WPF應用程序和DLL不存在於同一個文件夾

你提到需要爲您的應用程序訪問的DLL - 默認情況下它的外觀在同文件夾爲可執行文件。如果它找不到dll,它如何從庫中加載你使用的東西?

在Visual Studio中,在參考的屬性下確保「Copy Local」設置爲true。之後,爲了好的方法運行Build - > Clean Solution,然後Build - > Rebuild Solution。 MyLibrary.dll現在應該在發佈文件夾中,您不應該得到異常。

+0

「Copy Local」設置爲true。實際上,我指的是部署應用程序時的發佈情況,並且由於某些原因dll不存在。在使用庫中的任何功能之前,應檢查dll是否存在。這種情況應該是可以處理的權利? – tzippy

+0

它仍然不清楚,如果它的組件丟失是一個可能的,預期的應用程序狀態。如果這是預期的可能性,那麼你很可能需要做的不僅僅是處理異常,而且需要開始研究動態加載程序集。看到這裏:http://stackoverflow.com/questions/5198367/how-to-try-and-catch-assembly-not-found。也看看MEF – plast1k

+0

這很好地解決了我的問題。我現在正在研究AssemblyResolve事件。謝謝! – tzippy

0

問題是XAML分析器實際上並不會引用對引用程序集的引用,因此在構建時會將引用程序集刪除。構建鏈可以防止未使用的程序集被複制(例如默認引用的System.Core)。只需在代碼中添加對程序集的引用,它應該很好。

https://social.msdn.microsoft.com/Forums/vstudio/en-US/7f255570-dd53-41f8-b8c4-a160ba325c90/reference-not-loaded-into-assembly-when-only-using-xaml-for-the-referenced-code-bug?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]