WPF 应用程序启动时失败并出现 TypeInitializationException

发布于 2024-07-06 04:42:26 字数 829 浏览 12 评论 0原文

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

什么叫误入歧途?

I have a simple WPF application which I am trying to start. I am following the Microsoft Patterns and Practices "Composite Application Guidance for WPF". I've followed their instructions however my WPF application fails immediately with a "TypeInitializationException".

The InnerException property reveals that "The type initializer for 'System.Windows.Navigation.BaseUriHelper' threw an exception."

Here is my 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>

And here is my app.xaml.cs (exception thrown at "public App()"):

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

I have set the "App" class as the startup object in the project.

What is going astray?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

清醇 2024-07-13 04:42:26

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

看来问题是元素,因为当我删除它时,应用程序运行良好。 我很困惑,因为当我选中复选框以利用 3.5 SP1 中提供的“客户端配置文件”时,Visual Studio 2008 添加了这一点。

经过一番检查和取消选中该框后,我最终得到了一个如下的配置文件:

<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 中元素的顺序很重要 - 但看起来确实如此。

Thanks @ima, your answer pointed me in the right direction. I was using an app.config file and it contained this:

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

It seems the problem was the <startup> element because when I removed it the application ran fine. I was confused because Visual Studio 2008 added that when I checked the box to utilise the "Client Profile" available in 3.5 SP1.

After some mucking about checking and un-checking the box I ended up with a configuration file like this:

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

Which works!

I'm not sure why the order of elements in the app.config is important - but it seems it is.

-柠檬树下少年和吉他 2024-07-13 04:42:26

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

Anything wrong in the App.config file may cause the error, such as a typo of * at the end of a line, eg ...</startup> has an additional "*" at the end of the line ...</startup>*.

带上头具痛哭 2024-07-13 04:42:26

我遇到了类似的情况。
经过一周的搜索,我找到了解决方案,它确实对我有用。
它解决了由于同一问题而产生的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
“产品版本”=“3.0.6920.1453”
"WPFNonReferenceAssembliesPathx86"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\"

我确信它会起作用。

一切顺利。

问候,

乌梅什

I ran into a similar situation.
After searching for a week time, I found the resolution and it really worked for me.
It solved 2-3 problems arising due to same problem.

Follow these steps:
Check the WPF key (absence) in registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation
My problem was due to the absence of above mentioned key in registry.

You can modify and use following details in your registry: (Actually, you can save in file and import in registry)

[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\"

I am sure it will work.

all the best.

Regards,

Umesh

究竟谁懂我的在乎 2024-07-13 04:42:26

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

You have two sections named "modules". Place both module definitions in one section named "modules".

蒗幽 2024-07-13 04:42:26

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

Do you use .config file? If so, check it for errors. Initialization errors of such sort are often triggered by invalid XML: if there are no errors in XAML, XML config is the first place to look.

埖埖迣鎅 2024-07-13 04:42:26

深入跟踪 InnerExceptions,您可能会发现以下错误:

“每个配置文件仅允许一个元素,如果存在,则必须是根元素的第一个子元素”

Visual Studio EntityFramework 向导将 connectionStrings 元素添加到顶部后发生顺序更改

Tracking the InnerExceptions deep down , you might find the following error:

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

This order change happened after Visual Studio EntityFramework Wizard added the connectionStrings element to the top

浅忆 2024-07-13 04:42:26

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

If you only see the TypeInitializationException with no reason or no details on what's wrong, then disable Just My Code in the Visual Studio options.

一张白纸 2024-07-13 04:42:26

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

For me, I had copied app settings over from another application into my app.config into a new section called "userSettings". However, there needs to be a "configSections" also added to the app.config which defines "userSettings". I deleted the userSettings section then edited the app settings and saved it. VS automatically creates the correct "userSettings" and "configSections" for you if they are absent.

寒江雪… 2024-07-13 04:42:26

就我而言,需要添加:

<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

In my case this is need to be added:

<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" />

Section at App.config (VS 2015 .NET 4.5.2)

Open any WPF project what builded before, check build, if OK - check and compare App.config's at both projects

零時差 2024-07-13 04:42:26

对我来说,我重命名了我的应用程序名称并导致了此错误。 我有一个服务器和客户端应用程序。 服务器应用程序没有这个问题。 所以我检查了服务器和客户端的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>

For me I renamed my Application name and caused this error. I had a server and client app. the server app was not having this issue. so i checked App.config file of both server and client. I found

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

<startup> tag above <configSections> tag in client and server had the other way so I copy pasted startup tag down configSections tag and it worked. Like this.

<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>
与君绝 2024-07-13 04:42:26

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

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>

对我有用。

I was getting the same error. The suggestions mentioned above did not work for me. i was getting the following error after running

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)

I was getting an error in my exe.config file line 4. The .exe.config file was :

<?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>

However after trial and error i figured out that deleting the configSections

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

worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文