23

我有一个简单的 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”类设置为项目中的启动对象。

什么会误入歧途?

4

11 回答 11

37

谢谢@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>

似乎问题出在 <startup> 元素上,因为当我删除它时,应用程序运行良好。我很困惑,因为 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 中元素的顺序很重要——但似乎确实如此。

于 2008-09-12T07:51:05.803 回答
11

App.config文件中的任何错误都可能导致错误,例如在行尾的拼写错误,例如*在行尾...</startup>有一个额外的“*” ...</startup>*

于 2013-01-24T02:25:32.043 回答
8

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

于 2008-09-12T07:25:52.463 回答
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 Wizard 将 connectionStrings 元素添加到顶部之后

于 2016-04-28T20:12:16.060 回答
2

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

于 2016-09-22T15:02:28.770 回答
2

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

于 2017-09-08T22:36:31.313 回答
0

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

于 2009-06-18T10:48:51.700 回答
0

我遇到了类似的情况。在搜索了一周的时间后,我找到了解决方案,它确实对我有用。它解决了由于相同问题而出现的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" "Version"="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\ " "InstallSuccess"=dword:00000001 "ProductVersion"="3.0.6920.1453" "WPFNonReferenceAssembliesPathx86"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\"

我相信它会起作用。

祝一切顺利。

问候,

乌梅什

于 2010-05-20T16:59:33.137 回答
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 2015 .NET 4.5.2)

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

于 2017-02-19T00:13:42.830 回答
0

对我来说,我重命名了我的应用程序名称并导致了这个错误。我有一个服务器和客户端应用程序。服务器应用程序没有这个问题。所以我检查了服务器和客户端的 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> 标记上方的 <startup> 标记采用了另一种方式,因此我将粘贴的启动标记复制到 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>
于 2020-08-11T10:37:40.880 回答
0

我遇到了同样的错误。上面提到的建议对我不起作用。运行后我收到以下错误

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'System.Windows.Application' threw an exception.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Application..ctor()
   at ShortBarDetectionSystem.App..ctor()
   at ShortBarDetectionSystem.App.Main()

Inner Exception 1:
TypeInitializationException: The type initializer for 'System.Windows.Navigation.BaseUriHelper' threw an exception.

Inner Exception 2:
TypeInitializationException: The type initializer for 'MS.Internal.TraceDependencyProperty' threw an exception.

Inner Exception 3:
ConfigurationErrorsException: Configuration system failed to initialize

Inner Exception 4:
ConfigurationErrorsException: Section or group name 'oracle.manageddataaccess.client' is already defined. Updates to this may only occur at the configuration level where it is defined. (C:\ShortBarDetectionSystem\code\framework\TypeInitializationException\ver0_1\ShortBarDetectionSystem\ShortBarDetectionSystem\bin\x64\Debug\GrateBarDefectDetectionSystem.exe.Config line 4)

我的 exe.config 文件第 4 行出现错误。.exe.config 文件是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client" />
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

但是经过反复试验,我发现删除 configSections

<configSections>
    <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342" />
  </configSections>

为我工作。

于 2022-02-04T17:37:56.200 回答