【发布时间】:2018-02-23 12:16:12
【问题描述】:
您好,我的问题是关于 WPF XAML 解析异常的来源以及捕获它的方法。到目前为止,尽管从设置中添加了所有异常,但我无法捕获它。
当我尝试从默认样式派生样式时,它崩溃了。
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '13' and line position '22'.
If there is a handler for thisenter code here exception, the program may be safely continued.
样式应用的地方:
<Grid Margin="0,0,1,51">
<StackPanel Name="tbPanel" Margin="80,8,98,90" >
<TextBox Name="txtInput0" ></TextBox>
<TextBox Name="txtOutput0" Style="{StaticResource Custom}" ></TextBox>
</StackPanel>
</Grid>
资源部分:
<Window.Resources>
<Style TargetType="TextBox">
<Setter Property="Background" Value="DarkMagenta"></Setter>
</Style>
<Style TargetType="TextBox" x:Key="Custom" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Background" Value="Beige"></Setter>
</Style>
</Window.Resources>
我试图捕捉异常无济于事。我从异常设置中添加了所有异常,并在主窗口中添加了 Try-Catch 块,但仍然没有。
public MainWindow()
{
try
{
InitializeComponent();
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
}
}
PS:在 xaml 设计器中,尽管出现错误,但两个文本框的颜色不同。
【问题讨论】:
-
您的代码对我来说很好用。这两个 XAML sn-ps 肯定在同一个 .xaml 文件中吗?
-
感谢您的快速回复。这两个 sn-ps 在同一个 .xaml 文件中。
-
嗯。您是否尝试过旧的 close-VS/reopen/clean-build 例程?如果这不起作用,请尝试在新项目中重新创建它,直到您有一个可靠地重现问题的最小案例。事实上,我看到的所有代码都对我有用,所以很难提供帮助。
-
问题显然是资源部分是在资源用户之后声明的。
-
啊哈,是的,这样就行了。
标签: c# wpf xaml xml-parsing styles