2008-09-12 54 views
21

我有一個簡單的WPF應用程序,我試圖啓動。我遵循微軟模式和實踐「WPF複合應用程序指南」。我遵循他們的指示,但是我的WPF應用程序立即失敗,出現「TypeInitializationException」。使用TypeInitializationException啓動WPF應用程序失敗

InnerException屬性顯示「System.Windows.Navigation.BaseUriHelper的類型初始值設定項引發異常。」

這裏是我的App.xaml:

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

這裏是我的app.xaml.cs(例外,在 「公共應用程序()」 拋出):

public partial class App : Application 
{ 
    public App() 
    { 
     Bootstrapper bootStrapper = new Bootstrapper(); 
     bootStrapper.Run(); 
    } 
} 

我已經設置了「App」類作爲項目中的啓動對象。

發生了什麼事情?

回答

36

謝謝@ima,你的回答指向了我正確的方向。我使用的app.config文件,並將其包含在此:

<configuration> 
    <startup> 
    <supportedRuntime version="v2.0.50727" sku="Client"/> 
    </startup> 
    <configSections> 
    <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/> 
    </configSections> 
    <modules> 
    <module assemblyFile="Modules/MyNamespace.Modules.ModuleName.dll" moduleType="MyNamespace.Modules.ModuleName.ModuleClass" moduleName="Name"/> 
    </modules> 
</configuration> 

看來問題是<啓動>元素,因爲當我刪除它的應用程序運行得很好。我很困惑,因爲Visual Studio 2008補充說,當我檢查框以利用3.5 SP1中提供的「客戶端配置文件」。

經過一番擺弄檢查和取消選中我結束了這樣的配置文件的郵箱:

<configuration> 
    <configSections> 
    <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/> 
    </configSections> 
    <modules> 
    <module assemblyFile="Modules/MyNamespace.Modules.ModuleName.dll" moduleType="MyNamespace.Modules.ModuleName.ModuleClass" moduleName="Name"/> 
    </modules> 
    <startup> 
    <supportedRuntime version="v2.0.50727" sku="Client"/> 
    </startup> 
</configuration> 

其中一期工程!

我不知道爲什麼app.config中的元素順序很重要 - 但它似乎是。

+9

元素必須是第一個。其餘的順序是無關緊要的。 – Alex 2011-03-14 12:25:21

+0

如果我沒有記錯,它是Visual Studio本身,它通過插入元素來改變元素順序,因此我的困惑。 – 2011-03-15 06:28:05

8

你使用.config文件嗎?如果是這樣,請檢查它是否有錯誤。這種排序的初始化錯誤通常是由無效的XML觸發的:如果XAML中沒有錯誤,則XML config是第一個查找的地方。

0

您有兩個部分名爲「模塊」。將兩個模塊定義放在一個名爲「modules」的節中。

0

我遇到了類似的情況。 找了一週時間後,我找到了解決方案,它確實對我有用。 解決了同樣問題導致的2-3個問題。

遵循以下步驟: 檢查註冊表中的WPF鍵(無): HKEY_LOCAL_MACHINE \ SOFTWARE \微軟\ NET Framework安裝\新民主黨\ 3.0 \ SETUP \的Windows Presentation Foundation 我的問題是,由於缺乏上面提到的關鍵註冊表

您可以修改和使用按照您的註冊表信息:(其實,你可以在文件並導入註冊表中保存)

[HKEY_LOCAL_MACHINE \ SOFTWARE \微軟\ NET Framework安裝\新民主黨\ 3.0 \ SETUP \的Windows Presentation Foundation] @ = 「WPF v3.0.6920.1453」 「版本」= 「3.0.6920.1453」 「WPFReferenceAssembliesPathx86」=「C:\ Program Files文件\參考大會\微軟\框架\ V3。0「 」WPFCommonAssembliesPathx86「=」C:\ Windows \ System32 \「 」InstallRoot「=」C:\ Windows \ Microsoft.NET \ Framework \ v3.0 \ WPF \「 」InstallSuccess「= dword:00000001 「的ProductVersion」= 「3.0.6920.1453」 「WPFNonReferenceAssembliesPathx86」= 「C:\ WINDOWS \ Microsoft.NET \框架\ 3.0 \ WPF \」

我相信它會工作

所有。最好的。

問候,

Umesh製作

11

任何問題在的App.config文件可能會導致錯誤,如在每行的端部的*一個錯字,例如...</startup>有一個附加的「*」在線...</startup>*的末尾。

2

跟蹤InnerExceptions內心深處,你可能會發現以下錯誤:

"Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element"

這筆訂單的變更發生後的Visual Studio的EntityFramework嚮導添加的ConnectionStrings元素頂端

2

如果你只看到TypeInitializationException沒有理由或沒有詳細說明什麼是錯誤,然後在Visual Studio選項中禁用Just My Code。

0

在我而言,這是需要添加:

<configSections> 
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 

在App.config中(VS .NET 2015年4.5.2)

打開任何WPF項目之前建造什麼科檢查建立,如果OK - 檢查和比較的App.config的在這兩個項目

1

對於我來說,我已經在從另一個應用程序複製應用程序設置成我的app.config到一個名爲「userSettings」新欄目。但是,還需要將「configSections」添加到定義「userSettings」的app.config中。我刪除了userSettings部分,然後編輯了應用程序設置並保存了它。 VS自動爲你創建正確的「userSettings」和「configSections」,如果他們缺席。