【问题标题】:XamlParseException for some certain StaticResource on some certain computers某些特定计算机上某些特定静态资源的 XamlParseException
【发布时间】:2012-06-01 21:25:38
【问题描述】:

我们正在为 .NET 4 开发 WPF 应用程序。

有一天,客户告诉我新版本在他的 Windows 7 机器上不起作用(应用程序在启动时退出),他在 Windows 事件查看器中发现了异常日志:

    Application: myapp.exe Framework Version: v4.0.30319 Description: 
The process was terminated due to an unhandled exception. Exception Info: System.Windows.Markup.XamlParseException 
Stack: at System.Windows.Markup.XamlReader.RewrapException(System.Exception, System.Xaml.IXamlLineInfo, System.Uri) 
at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri) 
at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri) 
at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean) 
at System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext) 
at ... (I guess no point to continue...)

这个问题让我发疯,因为无论是我还是其他开发人员都无法在我们的机器上重现它,即使在 VirtualBox 中安装干净的 Windows 7 也无法重现它。

当我们试图找到破坏客户端应用程序的更改时,我们发现了有问题的部分。区别如下:

这很好用:

<ComboBox x:Name="comboBoxZoom" Margin="130,10,0,0" HorizontalAlignment="Left" Width="40" FontFamily="Arial" FontSize="12"  VerticalAlignment="Top" TabIndex="1" Panel.ZIndex="2" />

这会中断:

 <ComboBox x:Name="comboBoxZoom" Style="{StaticResource comboBoxStyle}" Margin="130,10,0,0" HorizontalAlignment="Left" Width="40" FontFamily="Arial" FontSize="12"  VerticalAlignment="Top" TabIndex="1" Panel.ZIndex="2" />

本质上只有区别

Style="{StaticResource comboBoxStyle}"

但是这种风格之前已经在我们应用程序的其他部分中使用过,没有任何问题!导致此异常的同一文件包含许多其他 Style="{StaticResource someotherstyle}" 并且它们在客户端计算机上工作正常。

所有这些样式都位于一个文件 ControlStyles.xaml 中。 comboBoxStyle 是从某个网站复制的修改样式,类似于这个: http://social.msdn.microsoft.com/Forums/nl/wpf/thread/53134b87-1a99-4998-a1fb-b3d8a9bd2773

为什么 Style="{StaticResource comboBoxStyle}" 使我的应用程序仅在某些特定机器上崩溃以及如何修复此错误?

【问题讨论】:

  • 您的客户是否向您发送了故障转储?使用 VS2010 故障转储调试器或使用 WinDbg 和 SOS,您可以找到 XamlParseException 的消息文本。该文本可能会为您提供有关问题原因的线索。
  • 如何为 .NET 4 应用程序生成转储?据此:msdn.microsoft.com/en-us/library/bb787181(VS.85).aspx .NET 转储无法自动生成。
  • 您为 .NET 应用程序创建转储的方式与为其他应用程序创建的方式相同。如果您在出现问题的机器上安装了 WinDbg,请将 WinDbg 附加到应用程序的进程,等待问题发生,然后执行命令.dump /mfth filename.dmp。或者,使用 Sysinternal 的 procdump 工具 (technet.microsoft.com/en-us/sysinternals/dd996900.aspx);确保指定 -ma-mp 以便故障转储包含完整内存。
  • 谢谢,伙计们。我的客户端没有 WinDbg,但我只是使用此示例实现了一个异常处理程序:social.msdn.microsoft.com/Forums/en-GB/clr/thread/…。让我们看看,现在它带我去哪里。

标签: wpf xaml exception staticresource


【解决方案1】:

我遇到了同样的问题,在拔掉头发两天后,我在 C# IRC 频道中的某个人的帮助下使用 Windbg 找到了解决方案。

所以在我的例子中,抛出异常是因为我没有为 app.config 的跟踪侦听器设置 type 属性,并且它与 XAML 无关,尽管奇怪的是,当我在我的 XAML 中删除了组合框绑定时,我可以在发布模式下运行程序,但真正的解决方案在于 app.config。不过,我可以在调试模式下正常运行程序。这听起来像是编译器的一个讨厌的错误。

因此,在您的 app.config 中寻找可能的疏忽,如果一切似乎都在那里,那么只需抓住 Windbg 并自行调试它,直到找到异常的根本原因。

【讨论】:

    猜你喜欢
    • 2019-11-23
    • 1970-01-01
    • 2022-11-26
    • 2021-10-06
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多