我有一種情況,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