Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

.net - WPF Type initialization Exception in C#

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.

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.

<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.

How can this problem be resolved?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...