在另一个Style xaml中使用StandardStyle xaml中的样式

时间:2012-11-07 03:35:51

标签: c# xaml windows-8 microsoft-metro

我有一个自定义的Color.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文件中使用这种颜色。

1 个答案:

答案 0 :(得分:1)

在这里,包括Color.xaml中的StandardStyle.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Color.xaml"/>    
    </ResourceDictionary.MergedDictionaries>

    <!-- your styles here -->

</ResourceDictionary>