我有一种情况,SolidColorBrush(在App.xaml中定义)在运行时无法解析,当我在样式中使用Brush作为StaticResource时。StaticResource not found
在设计时(使用Visual Studio 2010)找到画笔,因为当我更改画笔的颜色时,具有样式的UIElement用新颜色更新。
运行时期间引发XAMLParseException,找不到资源“color”。
这是正常行为吗?我认为StaticResource的解决方案从UIElements开始直到应用程序资源,并且应用程序资源是为应用程序的UIElements定义默认值(颜色,字体等)的好地方。
的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>
你有没有尝试添加的SolidColorBrush到Styles.XAML? – 2010-07-28 09:15:55
@Ardman:这有效。但多数民众赞成在不希望我想在这里 – Jehof 2010-07-28 09:24:06