.net WPF 应用程序在启动时失败,并出现 TypeInitializationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/58425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:48:59  来源:igfitidea点击:

WPF Application fails on startup with TypeInitializationException

.netwpfprism

提问by Adrian Clark

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

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

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

InnerException 属性显示“'System.Windows.Navigation.BaseUriHelper' 的类型初始值设定项引发异常。”

Here is my app.xaml:

这是我的 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()"):

这是我的 app.xaml.cs(在“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.

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

What is going astray?

什么会误入歧途?

回答by Adrian Clark

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

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

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.

问题似乎出在 <startup> 元素上,因为当我删除它时,应用程序运行良好。我很困惑,因为 Visual Studio 2008 添加了当我选中该框以使用 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.

我不确定为什么 app.config 中元素的顺序很重要 - 但似乎很重要。

回答by Lin Song Yang

Anything wrong in the App.configfile 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>*.

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

回答by ima

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.

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

回答by usefulBee

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

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

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

"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

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

回答by lvmeijer

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.

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

回答by Jason Cheng

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.

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

回答by Denis Kirin

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)

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

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

回答by Denis Kirin

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

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

回答by Umesh Bhavsar

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.

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

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.

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

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

[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

乌梅什