簡體   English   中英

在另一個Style xaml中使用StandardStyle xaml中的樣式

[英]Using style in StandardStyle xaml from another Style xaml

我有一個自定義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>
                ............

在My StandardStyle.xaml中,我無法使用xaml中定義的Color。

<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文件中使用此顏色。

這是,包括Color.xamlStandardStyle.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM