使用默认样式的派生样式时,WPF xaml解析异常

时间:2017-09-14 13:21:39

标签: c# wpf xaml xml-parsing styles

你好我的问题是关于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设计器中,尽管存在错误,但两个文本框的颜色不同。

1 个答案:

答案 0 :(得分:0)

问题是由于使用Window.Resource部分中使用的样式的UI元素在Resource部分之前声明了。

在UI元素解决问题之前移动参考资料部分。