C#中的WPF类型初始化异常

wri*_*tjm 10 .net c# wpf xaml

我有其他人的基于WPF的.NET 3.5应用程序,我正在尝试更新到.NET 4.5.该代码在.NET 3.5细跑了,而我在运行Visual Studio 2013快车在Windows 7的更新似乎顺利,代码编译罚款,但是当我尝试运行应用程序,我得到下面的异常.

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

Additional information: The type initializer for 'System.Windows.Application' threw an exception.
Run Code Online (Sandbox Code Playgroud)

以下是stacktrace中的最后几个步骤.

PresentationFramework.dll!System.Windows.Windows.Application()
MiniMon.exe!MiniMon.App.App()
MiniMon.exe!MiniMon.App.Main()
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

我可以下载一个示例WPF应用程序(WPFCalculator),它运行正常,所以我尝试将我正在更新的那个剥离到示例应用程序中的内容.我还尝试在app.xaml.cs中的代码的入口点添加断点,但是在执行该代码之前抛出了异常.作为最后的手段,我尝试在Windows 8上运行该应用程序,但得到了同样的错误.

如何解决这个问题?

Rez*_*M.A 29

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

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


Gur*_*Rao 8

迟到了,但在我的情况下发现了这个原因.我添加了key-value一对appSettingsin app.configin in

<appSettings>
    <add key="EncryKey" value="MyKey"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)

之前<configSections>.在互联网上挖掘后,我开始知道,<configSections>不得不处于最高位置root,不久之后<configuration>其余的订单都无关紧要.appSettings向下移动<configSections>帮助我解决了这个问题.

  • 这就是为我做的答案.日Thnx! (2认同)

use*_*Bee 6

深入研究Exception详细信息直到最后一次InnerException,我发现了这个:

"Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element"
Run Code Online (Sandbox Code Playgroud)

configSections移动到开头


Sha*_*aak 3

一种(不是很有教育意义)的解决方法是启动一个新的 4.5 项目,并从旧项目中复制粘贴相关代码片段。