【问题标题】:WPF Type initialization Exception in C#C# 中的 WPF 类型初始化异常
【发布时间】:2014-02-27 23:47:25
【问题描述】:

我有其他人的基于 WPF 的 .NET 3.5 应用程序,我正在尝试更新到 .NET 4.5。代码在 .NET 3.5 下运行良好,我在 Windows 7 上运行 Visual Studio 2013 Express。更新似乎进展顺利,代码编译良好,但当我尝试运行应用程序时,出现以下异常。

An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll

Additional information: The type initializer for 'System.Windows.Application' threw an exception.

这是堆栈跟踪的最后几个步骤。

PresentationFramework.dll!System.Windows.Windows.Application()
MiniMon.exe!MiniMon.App.App()
MiniMon.exe!MiniMon.App.Main()

这是 app.xaml 文件。

<Application x:Class="MiniMon.App"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>

我可以下载一个示例 WPF 应用程序 (WPFCalculator),它运行良好,因此我尝试将要更新的应用程序剥离为示例应用程序中的内容。我还尝试在 app.xaml.cs 中代码的入口点添加断点,但即使在执行该代码之前也会引发异常。作为最后的手段,我尝试在 Windows 8 上运行该应用程序,但遇到了同样的错误。

如何解决这个问题?

【问题讨论】:

  • 您能提供给我们 App.Main() 代码吗?
  • 您可能会检查所有引用(包括 .net 框架)并确保您仍然没有引用任何 3.5 框架程序集。
  • @DragomirRăzvan 这是 app.xaml.cs 的代码。我不太清楚为什么原作者没有添加标准初始化代码,但我尝试添加它仍然得到同样的错误。 using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Windows; namespace MiniMon { /// &lt;summary&gt; /// Interaction logic for App.xaml /// &lt;/summary&gt; public partial class App : Application { } }
  • 对不起,我是 stackoverflow 的新手。
  • 根据the documentation for the System.TypeInitializationException:“当类初始化程序无法初始化类型时,会创建一个 TypeInitializationException 并传递对该类型的类初始化程序抛出的异常的引用。TypeInitializationException 的 InnerException 属性包含潜在的例外。”你看过内部异常了吗?

标签: c# .net wpf xaml


【解决方案1】:

我通过将app.config中的启动部分移动到&lt;/configuration&gt;之前的最后部分来解决这个问题,启动部分必须是app.config中的最后部分,如下所示:

<startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>

【讨论】:

  • App.config 中的混乱也阻止了我启动 WPF 应用程序。
  • 我遇到了与 OP 完全相同的错误,最终是由 App.Config 中的语法错误引起的。似乎抛出的异常肯定是由 App.Config 出现问题引起的。
【解决方案2】:

聚会迟到了,但在我的情况下找到了这个原因。我在app.config 中添加了key-valueappSettings 对,如

<appSettings>
    <add key="EncryKey" value="MyKey"/>
</appSettings>

&lt;configSections&gt; 之前。在互联网上挖掘后,我知道&lt;configSections&gt; 必须在root 的顶部,紧随&lt;configuration&gt; 之后,其余订单无关紧要。将appSettings 移到&lt;configSections&gt; 下方帮助我解决了这个问题。

【讨论】:

  • 这是为我做的答案。谢谢!
【解决方案3】:

深入挖掘异常细节直到最后一个 InnerException,我发现了这个:

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

configSections 移到开头

【讨论】:

    【解决方案4】:

    一种(不是很有教育意义的)解决方法是开始一个新的 4.5 项目,然后从旧项目中复制粘贴相关代码。

    【讨论】:

    • 正如您提到的@shahar,这当然不是一个好的解决方案,但它是我最终不得不采用的解决方案。将所有内容复制到新的 .NET 4.5 项目后,应用程序无需任何代码更改即可运行。
    【解决方案5】:

    &lt;connectionStrings&gt; 标签应该在&lt;configSections&gt;&lt;startup&gt; 部分之后。这使我的代码工作。

    【讨论】:

      【解决方案6】:

      如果使用connectionStrings,connectionString="",请检查拼写! 'S' 应该大写 ..XML 区分大小写 :)

      【讨论】:

        【解决方案7】:

        检查 app.config 文件,确保您没有不再使用或已从项目中删除的 lib 配置

        【讨论】:

          【解决方案8】:

          我替换了已折旧的配置库并删除了实体框架,因此我根本无法跟踪问题所在,但重建帮助了我。解决方案 Rebuild your App.Config file.

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-07
            • 2012-07-07
            • 2013-04-03
            • 2012-02-01
            相关资源
            最近更新 更多