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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22083232/
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-13 11:03:09  来源:igfitidea点击:

WPF Type initialization Exception in C#

c#.netwpfxaml

提问by wrightjm

I have someone else's WPF-based .NET 3.5 app that I'm attempting to update to .NET 4.5. The code ran fine under .NET 3.5, and I'm running Visual Studio 2013 Express on Windows 7. The update seemed to go well and the code compiles fine, but when I try to run the app I get the following exception.

我有其他人的基于 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.

Here are the last few steps in the stacktrace.

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

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

Here's the app.xaml file.

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

I can download a sample WPF app (WPFCalculator) and it runs fine, so I tried stripping the one I'm updating down to just what was in the sample app. I also tried adding a breakpoint at the entry point of the code in app.xaml.cs, but the exception is thrown even before that code is executed. As a last resort I tried running the app on Windows 8, but got the same error.

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

How can this problem be resolved?

如何解决这个问题?

采纳答案by Shaaak

One (not very educational) workaround would be to start a new 4.5 project, and copy-paste the relevant pieces of code from the old one.

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

回答by Reza M.A

I solve this problem by moving startup section in app.config to the last part before </configuration>, startup section must be the last part in app.config, like this:

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

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

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

回答by Guruprasad Rao

Late to the party, but found this reason in my case. I had added a key-valuepair with appSettingsin app.configas in

聚会迟到了,但在我的案例中找到了这个原因。我已经添加了key-value一对appSettingsin app.configas in

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

before <configSections>. Upon digging in the internet, I came to know that, <configSections>had to be at the top of the root, soon after <configuration>and rest of the orders are irrelevant. Moving appSettingsbelow <configSections>helped me fix this issue.

之前<configSections>。在互联网上挖掘后,我开始知道,<configSections>必须在root,不久之后<configuration>和其余订单无关紧要。appSettings下面移动<configSections>帮助我解决了这个问题。

回答by usefulBee

Digging Deeper into the Exception Details down to the last InnerException, and I found this:

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

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

Move configSectionsto the beginning

configSections移到开头

回答by Akshay Kumar Sharma

<connectionStrings>tag should come after <configSections>and <startup>section. This made my code work.

<connectionStrings>标签应该在<configSections><startup>部分之后。这使我的代码工作。

回答by Lee Song

I replaced the depreciated Configuration library and also removed entity framework so I can't track what's wrong at all, but rebuilding helped me. Solution to Rebuild your App.Config file.

我替换了折旧的 Configuration 库并删除了实体框架,因此我根本无法跟踪出了什么问题,但是重建对我有帮助。重建 App.Config 文件的解决方案 。

回答by Mbithy Mbithy

Check app.config file make sure that you don't have configurations to lib(s) that you no longer use or you have removed from the project

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

回答by David_Shervin

Check the spellings of connectionStrings,connectionString=""if used! 'S' should be capital ..XML is case sensitive :)

检查拼写 connectionStringsconnectionString=""如果使用!'S' 应该是大写的 ..XML 区分大小写 :)