StaticResource not found

I have a situation where SolidColorBrush (defined in App.xaml) cannot be resolved at runtime when I use Brush in style as StaticResource.

At design time (using Visual Studio 2010), the brush was found because when I change the color of the brush, the UIElement with styles is updated with a new color.

At run time, an XAMLParseException is thrown that the color resource cannot be found.

Is this normal behavior? I thought that StaticResource resolution starts from UIElements resources to Application resources and that application resources are a good place to define default values ​​(colors, fonts, etc.) for UIElements applications.

App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         x:Class="SilverlightApplication1.App"
         >
<Application.Resources>
    <ResourceDictionary>
        <SolidColorBrush Color="Green" x:Key="color"/>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Styles.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style TargetType="Border">
    <Setter Property="BorderBrush" Value="{StaticResource color}" />
    <Setter Property="BorderThickness" Value="1" />
</Style>

Main.xaml

<UserControl x:Class="SilverlightApplication1.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <Border Height="100" HorizontalAlignment="Left" Margin="130,146,0,0" Name="border1" VerticalAlignment="Top" Width="200" />
</Grid>

+3
2

SolidColorBrush "" ResourceDictionary "General.xaml"

ResourceDictionary "General.xaml" ResourceDictionary "Styles.xaml". . , .

General.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <SolidColorBrush Color="Green" x:Key="color"/>
</ResourceDictionary>

Styles.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="General.xaml"/>
  </ResourceDictionary.MergedDictionaries>

  <Style TargetType="Border">
    <Setter Property="BorderBrush" Value="{StaticResource color}" />
    <Setter Property="BorderThickness" Value="1" />
  </Style>
</ResourceDictionary>
+2

, XAML . XAML .

App.xaml.cs

public App()
{
    if (DesignerProperties.IsInDesignTool == false)
    {
        this.Startup += this.Application_Startup;
    }
}

private void Application_Startup(object sender, StartupEventArgs e)
{           
    System.Uri uri = new System.Uri("/MRW.UI;component/Resources/Library.xaml", UriKind.RelativeOrAbsolute);

    System.Windows.ResourceDictionary dictionary = new System.Windows.ResourceDictionary(); 
dictionary.Source = uri;
    App.Current.Resources.MergedDictionaries.Add(dictionary);
}
+2

Source: https://habr.com/ru/post/1756898/


All Articles