2016-11-16 134 views
1

可以merged resource dictionariesApp.xaml訪問資源?我們的目標是分割風格,使其更具可讀性。合併資源字典可以從App.xaml訪問資源嗎?

這就是我要找的,但是以這種方式行不通:

的App.xaml在UWP項目

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles\DefaultButtonStyle.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 

    <!-- other custom styles, definitions, ThemeDictionaries, ... --> 
    <Color x:Key="Primary">#dfdfdf</Color> 
</Application.Resources> 

DefaultButtonStyle.xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:AppName.UWP.Styles"> 

    <!-- some definitions --> 
    <Style TargetType="Button" x:Key="DefaultButtonStyle"> 
     <!-- my styles --> 
     <Setter Property="Background" Value="{StaticResource Primary}" /> 
    </Style> 
</ResourceDictionary> 

該應用程序崩潰

無法找到名稱/鍵主

資源,我可以把一切都放在一個大style.xaml,或在每個XAML文件複製所需的值,但是是不是有其他的選擇嗎?合併的字典是否可以包含另一個合併的字典?或類似的東西?

回答

1

我已經使用分開的字典,並試圖讓他們在使用順序。在我的應用程序有:

  • ColorsAndBrushes.xaml
  • SizesAndLayout.xaml
  • DefaultStyles.xaml
  • NamedStyles.xaml

凡ColorsAndBrushes看起來像:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App.Styles"> 
    <!-- Colors --> 
    <Color x:Key="Color_Banner">#FF333232</Color> 
    <!--overridden from themeresource--> 
    <Color x:Key="SystemChromeDisabledLowColor">#FFA8A49F</Color> 
    <Color x:Key="SystemAccentColor">#FF2877CF</Color> 
    <!-- /Colors --> 

    <!-- Brushes --> 
    <SolidColorBrush x:Key="Brush_Banner" Color="{StaticResource Color_Banner}" /> 
    <!-- /Brushes --> 
</ResourceDictionary> 

尺寸和佈局:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App.Styles"> 
    <!-- Padding --> 
    <Thickness x:Key="Padding_Button">24,4</Thickness> 
    <Thickness x:Key="Padding_Dialog">10</Thickness> 
    <Thickness x:Key="Padding_Content">20</Thickness> 
    <!-- /Padding --> 

    <!-- Fonts --> 
    <FontFamily x:Key="Font_DefaultFamily">Segoe UI</FontFamily> 
    <FontWeight x:Key="Font_DefaultWeight">SemiLight</FontWeight> 
    <FontWeight x:Key="Font_NormalWeight">Normal</FontWeight> 
    <FontWeight x:Key="Font_BoldWeight">Semibold</FontWeight> 
    <x:Double x:Key="ContentControlFontSizeSmall">11</x:Double> 
    <x:Double x:Key="Font_NormalSize">20</x:Double> 
    <x:Double x:Key="Font_H1Size">36</x:Double> 
    <x:Double x:Key="Font_H2Size">28</x:Double> 
    <!-- /Fonts --> 
</ResourceDictionary> 

DefaultStyles(適用於所有類型的 - 其他2這些資源使用):

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App.Styles"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="ColorsAndBrushes.xaml" /> 
     <ResourceDictionary Source="SizesAndLayout.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 

    <Style TargetType="TextBlock"> 
     <Setter Property="FontFamily" Value="{StaticResource Font_DefaultFamily}" /> 
     <Setter Property="FontWeight" Value="{StaticResource Font_DefaultWeight}" /> 
     <Setter Property="FontSize" Value="{StaticResource Font_NormalSize}" /> 
     <Setter Property="TextWrapping" Value="WrapWholeWords" /> 
    </Style> 
</ResourceDictionary> 

和NamedStyles是默認的覆蓋:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App.Styles"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="ColorsAndBrushes.xaml" /> 
     <ResourceDictionary Source="SizesAndLayout.xaml" /> 
     <ResourceDictionary Source="DefaultStyles.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
    <Style x:Key="FontStyle_H1" TargetType="TextBlock" BasedOn="{StaticResource FontStyle_Default}"> 
     <Setter Property="FontSize" Value="{StaticResource Font_H1Size}" /> 
     <Setter Property="Foreground" Value="{StaticResource Brush_DarkBlue}" /> 
     <Setter Property="Margin" Value="{StaticResource Margin_TitleFont}" /> 
    </Style> 
</ResourceDictionary> 

最後,在App.xaml:

<Application 
    x:Class="MyApp.App.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:MyApp.App" 
    RequestedTheme="Light"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Styles/ColorsAndBrushes.xaml" /> 
       <ResourceDictionary Source="Styles/SizesAndLayout.xaml" /> 
       <ResourceDictionary Source="Styles/DefaultStyles.xaml" /> 
       <ResourceDictionary Source="Styles/NamedStyles.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

它適用於我,並通過使用較小範圍的文件保持較小的XAML文件。但是,我會說有時候Visual Studio會給我一些額外的錯誤,抱怨它不能找出命名空間......但是隻有在文件打開的時候。 我也經歷過,在運行時,靜態資源聲明的順序並不重要,有時,Visual Studio中的設計器不會呈現樣式,如果它們不是自上而下的格式。

祝你好運!

+0

我學到的東西: - 使用正斜槓('/')。 - 在XAML中進行更改,重新編譯,反轉更改,重新編譯,然後應用樣式。 – testing

0

試試這個:

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles\DefaultButtonStyle.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
     <!-- other custom styles, definitions, ThemeDictionaries, ... --> 
     <Color x:Key="Primary">#dfdfdf</Color> 
    </ResourceDictionary> 
</Application.Resources> 
+0

這似乎並不奏效。我得到多個錯誤,例如*「Key」屬性只能用於「IDictionary」。*等內容中包含的元素。 – testing