我有一個自定義Color.xaml作爲從另一個風格XAML中StandardStyle XAML使用樣式
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="MyColor1" Color="#7d897d"/>
<SolidColorBrush x:Key="MyColor2" Color="#078ab4"/>
</ResourceDictionary>
而且作爲App.xaml中
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Defines the colors used in the app-->
<ResourceDictionary Source="Color.xaml"/>
<!-- Styles that define common aspects of the platform look and
feel Required by Visual Studio project and item templates-->
<ResourceDictionary Source="StandardStyles.xaml"/>
<ResourceDictionary>
............
在我StandardStyle.xaml我不能夠使用xaml中定義的顏色。
<Style x:Key="HeadingTextStyle" TargetType="TextBlock">
<Setter Property="FontWeight" Value="SemiLight"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Foreground" Value="{StaticResource MyColor1}"/>
</Style>
它給了我一個例外,當我運行的代碼
"Cannot find a Resource with the Name/Key MyColor1 [Line: 20 Position: 44]"
但是我能夠在UI XAML文件使用這種顏色。
StandardStyle.Xaml是一個資源字典?如果是,那麼你有沒有在StandardStyle中合併你的色彩資源? –
不,我沒有將它合併到'StandardStyle.xaml'中,而是'App.xaml'中,並且是'StandardStyle.Xaml'是一個資源字典。 –