简体   繁体   中英

WPF xaml parse exception when using derived style from default one

Hello my question is about the source of a WPF XAML parse exception and what are the ways to catch it.So far despite adding all exceptions from the settings i can't catch it.

It crashes when i try to derive a style from the default one.

 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.

The place where the styles are applied:

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

The resources section:

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

I have tried to no avail to catch the exception.I added all exceptions from Exception settings and added Try-Catch blocks in the main window but still nothing.

  public MainWindow()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException.Message);
            }
        }

PS: In the xaml designer the two textboxes have different colors despite the error.

The problem was due to the fact that the UI elements that were using the styles used in the Window.Resource section were declared before the Resource section.

Moving the Resources section before the UI elements solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM