我有其他人基于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.
以下是stacktrace中的最后几个步骤。
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上运行应用程序,但出现了同样的错误。
如何解决这个问题?
答案 0 :(得分:28)
我通过将app.config中的启动部分移到</configuration>
之前的最后一部分来解决这个问题,启动部分必须是app.config中的最后一部分,如下所示:
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
答案 1 :(得分:8)
晚会,但在我的情况下找到了这个理由。我在key-value
中添加了appSettings
app.config
对与
<appSettings>
<add key="EncryKey" value="MyKey"/>
</appSettings>
在<configSections>
之前。在互联网上挖掘后,我发现<configSections>
必须位于root
的顶部,<configuration>
后不久,其余的订单无关紧要。将appSettings
移到<configSections>
以下帮助我解决了这个问题。
答案 2 :(得分:5)
深入挖掘Exception详细信息,直到最后一次InnerException,我发现了这个:
"Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element"
将 configSections 移至开头
答案 3 :(得分:2)
一个(不是非常有教育意义的)解决方法是启动一个新的4.5项目,并复制旧的代码片段。
答案 4 :(得分:1)
<connectionStrings>
标记应位于<configSections>
和<startup>
部分之后。这使我的代码工作。
答案 5 :(得分:0)
如果使用,请检查connectionStrings
,connectionString=""
的拼写!
'S'应该是大写..XML区分大小写:)
答案 6 :(得分:0)
检查app.config文件,确保您没有不再使用或已从项目中删除的lib的配置
答案 7 :(得分:0)
我更换了折旧的配置库,并删除了实体框架,因此我根本无法追踪错误,但重建帮助了我。解决方案 Rebuild your App.Config file