【问题标题】:WPF Application fails on startup with TypeInitializationExceptionWPF 应用程序在启动时失败并出现 TypeInitializationException
【发布时间】:2010-09-08 16:12:55
【问题描述】:

我有一个简单的 WPF 应用程序,我正在尝试启动它。我正在遵循 Microsoft 模式和实践“WPF 的复合应用程序指南”。我已按照他们的说明进行操作,但是我的 WPF 应用程序立即失败并出现“TypeInitializationException”。

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 App()”处抛出异常):

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

我已将“App”类设置为项目中的启动对象。

怎么回事?

【问题讨论】:

    标签: .net wpf prism


    【解决方案1】:

    您使用 .config 文件吗?如果是这样,请检查它是否有错误。此类初始化错误通常由无效的 XML 触发:如果 XAML 中没有错误,则首先查看 XML 配置。

    【讨论】:

      【解决方案2】:

      感谢@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 中的元素顺序很重要 - 但似乎很重要。

      【讨论】:

      • 元素必须是第一个元素。其余的顺序无关紧要。
      • 如果我没记错的话,是 Visual Studio 本身通过插入 元素改变了元素顺序,因此我很困惑。
      • 出现了完全相同的错误,正如 Alex 所说,将我的 configSections 移到我的配置顶部并且现在可以正常工作。由从客户端配置文件更改为仅 .Net 4 引起的。
      【解决方案3】:

      您有两个名为“模块”的部分。将两个模块定义放在一个名为“modules”的部分中。

      【讨论】:

        【解决方案4】:

        我遇到了类似的情况。 在搜索了一周的时间后,我找到了解决方案,它确实对我有用。 它解决了由于相同问题而出现的2-3个问题。

        按照以下步骤操作: 检查注册表中的 WPF 键(不存在): HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation 我的问题是由于注册表中没有上述键。

        您可以在注册表中修改和使用以下详细信息:(实际上,您可以保存在文件中并导入注册表)

        [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation] @="WPF v3.0.6920.1453" “版本”=“3.0.6920.1453” "WPFReferenceAssembliesPathx86"="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\" "WPFCommonAssembliesPathx86"="C:\Windows\System32\" "InstallRoot"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\" “安装成功”=dword:00000001 "ProductVersion"="3.0.6920.1453" "WPFNonReferenceAssembliesPathx86"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\"

        我相信它会起作用的。

        一切顺利。

        问候,

        乌梅什

        【讨论】:

          【解决方案5】:

          App.config 文件中的任何错误都可能导致该错误,例如在行尾的拼写错误*,例如...&lt;/startup&gt; 在行尾多了一个“*”行尾...&lt;/startup&gt;*

          【讨论】:

          • 完美。你拯救了我的一天。我不知道这个异常发生了什么。我不小心在我的 app.config 中输入了“4”,这就是异常背后的原因。谢谢。
          【解决方案6】:

          深入追踪InnerExceptions,你可能会发现如下错误:

          "Only one &lt;configSections&gt; element allowed per config file and if present must be the first child of the root &lt;configuration&gt; element"

          此顺序更改发生在 Visual Studio EntityFramework Wizard 将 connectionStrings 元素添加到顶部之后

          【讨论】:

            【解决方案7】:

            如果您只看到 TypeInitializationException 而没有任何原因或没有详细说明问题所在,请在 Visual Studio 选项中禁用“仅我的代码”。

            【讨论】:

              【解决方案8】:

              就我而言,这是需要添加的:

              <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 2015 .NET 4.5.2)

              打开之前构建的任何 WPF 项目,检查构建,如果正常 - 检查并比较两个项目中的 App.config

              【讨论】:

                【解决方案9】:

                对我来说,我已将应用设置从另一个应用程序复制到我的 app.config 到一个名为“userSettings”的新部分中。但是,还需要将“configSections”添加到定义“userSettings”的 app.config 中。我删除了 userSettings 部分,然后编辑了应用程序设置并保存了它。如果不存在,VS 会自动为您创建正确的“userSettings”和“configSections”。

                【讨论】:

                  【解决方案10】:

                  对我来说,我重命名了我的应用程序名称并导致了此错误。我有一个服务器和客户端应用程序。服务器应用程序没有这个问题。所以我检查了服务器和客户端的 App.config 文件。我找到了

                  <startup>
                  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
                  </startup>
                  
                  <configSections>
                  <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
                  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
                  </configSections>
                  
                  客户端和服务器中 标记上方的

                  标记有另一种方式,因此我将粘贴的启动标记复制到 configSections 标记下并且它起作用了。像这样。

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2018-06-25
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-04-04
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多