WPF 应用程序启动时失败并出现 TypeInitializationException
我有一个简单的 WPF 应用程序,我正在尝试启动它。 我遵循 Microsoft 模式和实践“WPF 复合应用程序指南”。 我已按照他们的说明进行操作,但是我的 WPF 应用程序立即失败并出现“TypeInitializationException”。
InnerException 属性显示“‘System.Windows.Navigation.BaseUriHelper’的类型初始值设定项引发了异常。”
这是我的 app.xaml:
<Application x:Class="MyNamespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
这是我的 app.xaml.cs(在“public App()”处抛出异常):
public partial class App : Application
{
public App()
{
Bootstrapper bootStrapper = new Bootstrapper();
bootStrapper.Run();
}
}
我已将“App”类设置为项目中的启动对象。
什么叫误入歧途?
I have a simple WPF application which I am trying to start. I am following the Microsoft Patterns and Practices "Composite Application Guidance for WPF". I've followed their instructions however my WPF application fails immediately with a "TypeInitializationException".
The InnerException property reveals that "The type initializer for 'System.Windows.Navigation.BaseUriHelper' threw an exception."
Here is my app.xaml:
<Application x:Class="MyNamespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
And here is my app.xaml.cs (exception thrown at "public App()"):
public partial class App : Application
{
public App()
{
Bootstrapper bootStrapper = new Bootstrapper();
bootStrapper.Run();
}
}
I have set the "App" class as the startup object in the project.
What is going astray?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
谢谢@ima,你的回答为我指明了正确的方向。 我正在使用 app.config 文件,它包含以下内容:
看来问题是元素,因为当我删除它时,应用程序运行良好。 我很困惑,因为当我选中复选框以利用 3.5 SP1 中提供的“客户端配置文件”时,Visual Studio 2008 添加了这一点。
经过一番检查和取消选中该框后,我最终得到了一个如下的配置文件:
哪个有效!
我不确定为什么 app.config 中元素的顺序很重要 - 但看起来确实如此。
Thanks @ima, your answer pointed me in the right direction. I was using an app.config file and it contained this:
It seems the problem was the <startup> element because when I removed it the application ran fine. I was confused because Visual Studio 2008 added that when I checked the box to utilise the "Client Profile" available in 3.5 SP1.
After some mucking about checking and un-checking the box I ended up with a configuration file like this:
Which works!
I'm not sure why the order of elements in the app.config is important - but it seems it is.
App.config 文件中的任何错误都可能导致该错误,例如行尾的
*
拼写错误,例如...
在行尾有一个额外的“*”...*
。Anything wrong in the App.config file may cause the error, such as a typo of
*
at the end of a line, eg...</startup>
has an additional "*" at the end of the line...</startup>*
.我遇到了类似的情况。
经过一周的搜索,我找到了解决方案,它确实对我有用。
它解决了由于同一问题而产生的2-3个问题。
按着这些次序:
检查注册表中的 WPF 密钥(不存在):
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation
我的问题是由于注册表中缺少上述密钥。
您可以在注册表中修改和使用以下详细信息:(实际上,您可以保存在文件中并导入到注册表中)
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation]
@=“WPF v3.0.6920.1453”
“版本”=“3.0.6920.1453”
"WPFReferenceAssembliesPathx86"="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\"
“WPFCommonAssembliesPathx86”=“C:\Windows\System32\”
“InstallRoot”=“C:\Windows\Microsoft.NET\Framework\v3.0\WPF\”
“安装成功”=dword:00000001
“产品版本”=“3.0.6920.1453”
"WPFNonReferenceAssembliesPathx86"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\"
我确信它会起作用。
一切顺利。
问候,
乌梅什
I ran into a similar situation.
After searching for a week time, I found the resolution and it really worked for me.
It solved 2-3 problems arising due to same problem.
Follow these steps:
Check the WPF key (absence) in registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation
My problem was due to the absence of above mentioned key in registry.
You can modify and use following details in your registry: (Actually, you can save in file and import in registry)
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation]
@="WPF v3.0.6920.1453"
"Version"="3.0.6920.1453"
"WPFReferenceAssembliesPathx86"="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\"
"WPFCommonAssembliesPathx86"="C:\Windows\System32\"
"InstallRoot"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\"
"InstallSuccess"=dword:00000001
"ProductVersion"="3.0.6920.1453"
"WPFNonReferenceAssembliesPathx86"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\"
I am sure it will work.
all the best.
Regards,
Umesh
您有两个名为“模块”的部分。 将两个模块定义放在名为“modules”的部分中。
You have two sections named "modules". Place both module definitions in one section named "modules".
您使用 .config 文件吗? 如果是这样,请检查是否有错误。 此类初始化错误通常是由无效的 XML 触发的:如果 XAML 中没有错误,则首先要查看 XML 配置。
Do you use .config file? If so, check it for errors. Initialization errors of such sort are often triggered by invalid XML: if there are no errors in XAML, XML config is the first place to look.
深入跟踪 InnerExceptions,您可能会发现以下错误:
“每个配置文件仅允许一个元素,如果存在,则必须是根元素的第一个子元素”
Visual Studio EntityFramework 向导将 connectionStrings 元素添加到顶部后发生顺序更改
Tracking the InnerExceptions deep down , you might find the following error:
"Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element"
This order change happened after Visual Studio EntityFramework Wizard added the connectionStrings element to the top
如果您只看到 TypeInitializationException,没有原因或没有详细说明问题所在,请在 Visual Studio 选项中禁用“仅我的代码”。
If you only see the TypeInitializationException with no reason or no details on what's wrong, then disable Just My Code in the Visual Studio options.
对我来说,我已将应用程序设置从另一个应用程序复制到我的 app.config 中一个名为“userSettings”的新部分。 但是,还需要将一个“configSections”添加到定义“userSettings”的app.config中。 我删除了 userSettings 部分,然后编辑了应用程序设置并保存了它。 如果“userSettings”和“configSections”不存在,VS 会自动为您创建正确的“userSettings”和“configSections”。
For me, I had copied app settings over from another application into my app.config into a new section called "userSettings". However, there needs to be a "configSections" also added to the app.config which defines "userSettings". I deleted the userSettings section then edited the app settings and saved it. VS automatically creates the correct "userSettings" and "configSections" for you if they are absent.
就我而言,需要添加:
App.config 部分(VS 2015 .NET 4.5.2)
打开之前构建的任何 WPF 项目,检查构建,如果正常 - 检查并比较两个项目中的 App.config
In my case this is need to be added:
Section at App.config (VS 2015 .NET 4.5.2)
Open any WPF project what builded before, check build, if OK - check and compare App.config's at both projects
对我来说,我重命名了我的应用程序名称并导致了此错误。 我有一个服务器和客户端应用程序。 服务器应用程序没有这个问题。 所以我检查了服务器和客户端的App.config文件。 我找到了
<启动> 上面的标签客户端和服务器中的标记有另一种方式,因此我将粘贴的启动标记复制到 configSections 标记中,它起作用了。 像这样。
For me I renamed my Application name and caused this error. I had a server and client app. the server app was not having this issue. so i checked App.config file of both server and client. I found
<startup> tag above <configSections> tag in client and server had the other way so I copy pasted startup tag down configSections tag and it worked. Like this.
我遇到了同样的错误。 上面提到的建议对我来说不起作用。 我在运行后收到以下错误
我在 exe.config 文件第 4 行中收到错误。.exe.config 文件是:
但是经过反复试验,我发现删除 configSections
对我有用。
I was getting the same error. The suggestions mentioned above did not work for me. i was getting the following error after running
I was getting an error in my exe.config file line 4. The .exe.config file was :
However after trial and error i figured out that deleting the configSections
worked for me.