2012-11-07 65 views
1

我有一個自定義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文件使用這種顏色。

+0

StandardStyle.Xaml是一個資源字典?如果是,那麼你有沒有在StandardStyle中合併你的色彩資源? –

+0

不,我沒有將它合併到'StandardStyle.xaml'中,而是'App.xaml'中,並且是'StandardStyle.Xaml'是一個資源字典。 –

回答

1

這是包括在StandardStyle.xamlColor.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> 
+0

你還能看到異常嗎? –

+0

不,我現在沒有得到例外:) –