我遇到这样的情况:当我以样式使用solidColorBrush作为staticResource时,在运行时无法解析solidColorBrush(在app.xaml中定义)。
在设计期间(使用Visual Studio 2010),会发现画笔,因为当我更改画笔的颜色时,样式为的ui元素将更新为新颜色。
在运行时引发xamlparseException,但找不到资源“color”。
这是正常行为吗?我认为静态资源的解析,从ui元素开始到应用程序资源,并且应用程序资源是为应用程序的ui元素定义默认值(颜色、字体等)的好地方。
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>
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>
MAM.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>