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开发者_Go百科="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>
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">
<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>
I recently had some success circumventing issues like these by placing all the styles into XAML resource dictionaries and merging them at runtime. Merging these same dictionaries in XAML did not solve the issue.
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);
}
精彩评论