In “System. Windows. StaticResourceExtension” provide caused abnormal values
As needed, a converter is written and referenced on the form, resulting in this error. It’s fine at compile time, but not at run time.
\
Could it be that the converter has written wrong? Breakpoint debugging finds that the converter code has not been run at all. On closer inspection, the system reports a XAML interpretation error, which is purely a problem here on the page.
\
The page code looks like this:
\
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cvt="clr-namespace:Converters"
IsEnabled="{Binding HasShowedUser,Converter={StaticResource cvtBool},Mode=OneWay}">
<Window.Resources>
<cvt:GenericTypeConverter x:Key="cvtBool"/>
</Window.Resources>
<Grid>
<Button x:Name="btnShowUser" Content="Show it off." Height="50" Width="100" Click="btnShow_Click" />
</Grid>
</Window>
Copy the code
\
Searching and searching, desolate and sad, not to ride a hundred tear sister.
\
Later will
IsEnabled="{Binding HasShowedUser,Converter={StaticResource cvtBool},Mode=OneWay}">
Copy the code
This line moves from <Window> to <Button>, and everything works fine.
\
This indicates that the previous error was due to the resource declaration after, and my call before, so the error was reported.
\
How to do? Can’t <Window> be used?
\
The solution is to put the resource declaration in a separate XAML file, such as staticreSources.xaml, and then include this staticresources.xaml in app.xaml, and then you can use these resources on your form.
\
StaticResources.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cvt="clr-namespace:Converters"
>
<cnvtrs:GenericTypeConverter x:Key="cvtBool" />
</ResourceDictionary>
Copy the code
app.xaml
<Application
x:Class="My.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="TestWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
<ResourceDictionary Source="StaticResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Copy the code
Call the form
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
IsEnabled="{Binding HasShowedUser,Converter={StaticResource cvtBool},Mode=OneWay}">
<Grid>
<Button x:Name="btnShowUser" Content="Show it off." Height="50" Width="100" Click="btnShow_Click" />
</Grid>
</Window>
Copy the code
\
\