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

什么叫误入歧途?

有帮助吗?

解决方案

谢谢@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> 元素上,因为当我删除它时,应用程序运行良好。我很困惑,因为当我选中复选框以利用 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 中元素的顺序很重要 - 但看起来确实如此。

其他提示

如有任何错误 应用程序配置 文件可能会导致错误,例如拼写错误 * 在一行的末尾,例如 ...</startup> 行尾有一个额外的“*” ...</startup>*.

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

深入跟踪 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 元素添加到顶部之后

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

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

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

我遇到了类似的情况。经过一周的搜索,我找到了解决方案,它确实对我有用。它解决了由于同一问题而产生的2-3个问题。

按着这些次序:检查注册表中的 WPF 密钥(不存在):HKEY_LOCAL_MACHINE SOFTWORD MICROSOFT Net Framework Setup ndp v3.0 setup setup Windows呈现基础我的问题是由于注册表中没有上述密钥。

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

HKEY_LOCAL_MACHINE SOFTWORS MICROSOFT NET FRAMERWOWS设置 ndp v3.0 setup Windows Windows theStrion foundation] @=“ wpf v3.0.6920.1453” =“版本” =“ 3.0.6920.1453”程序文件参考汇编 Microsoft Framework v3.0 “” WPFCommonAssembliesPathx86“ =” C: Windows System32 “”“ installroot” =“ C: Windows Microsoft.net.net.net framework framework v3.0 v3.0 wpf wpf wpf wpf ““ stermastsuccess” = dword:00000001“ productersion” =“ 3.0.6920.1453”“ wpfnonreferenceassembliespathx86” =“ c: windows microsoft.net framework fracework fracework v3.0 v3.0 wpf wpf ”

我确信它会起作用。

一切顺利。

问候,

乌梅什

就我而言,需要添加:

<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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top