views:

83

answers:

1

I have the situation where a SolidColorBrush (defined in App.xaml) cannot be resolved during runtime, when i use the Brush in a Style as StaticResource.

During designtime (using Visual Studio 2010) the brush is found, cause when i change the color of the brush the UIElement with the styles are updated with the new color.

During runtime a XAMLParseException is raised, that the resource "color" cannot be found.

Is this a normal behavior? I thought the resolving of StaticResource, starts from the UIElements up to the Application resources and that the Application resources are a good place to define default values (Colors, Fonts, etc.) for the UIElements of the Application.

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"&gt;

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

A: 

I refactored the resource definitions and put the SolidColorBrush "Color" in a ResourceDictionary "General.xaml"

I merged the ResourceDictionary "General.xaml" in the ResourceDictionary "Styles.xaml". Now it works without any problems. I think this is the way to go with resources.

General.xaml

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

  <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"&gt;

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

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