2017-08-11 70 views
0

我正在创建一个包含自定义控件的UWP库,并且我想为我的控件定义一个黑暗和轻盈的主题。我知道我们可以将主题资源字典添加到App.xaml。我们可以在那里指定一个黑暗/浅色主题,并设置请求的主题属性。但是因为我正在创建一个库,所以我的项目中没有App.xaml。在图书馆项目中定义一个主题对我来说可行吗?怎么样?如何在UWP库中定义主题?

回答

0

您可以通过使用添加>新项...>资源字典从项目菜单选项来创建Microsoft Visual Studio中的资源字典文件。然后我们可以在ResourceDictionary文件中定义ThemeDictionaries

例如:

<ResourceDictionary 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="using:ClassLibrary1"> 
     <ResourceDictionary.ThemeDictionaries> 
      <ResourceDictionary x:Key="Light"> 
       <SolidColorBrush x:Key="CustomColor" Color="Orange" /> 
      </ResourceDictionary> 
      <ResourceDictionary x:Key="Dark"> 
       <SolidColorBrush x:Key="CustomColor" Color="Blue" /> 
      </ResourceDictionary> 
      <ResourceDictionary x:Key="HighContrast"> 
       <SolidColorBrush x:Key="CustomColor" Color="Green" /> 
      </ResourceDictionary> 
     </ResourceDictionary.ThemeDictionaries> 
    </ResourceDictionary> 

要使用字典,我们可以控制的字典里把它合并:

<UserControl 
    x:Class="ClassLibrary1.MyUserControl1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:ClassLibrary1" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="400"> 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Dictionary1.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </UserControl.Resources> 
    <Grid> 
     <Rectangle Fill="{ThemeResource CustomColor}" Width="500" Height="500"></Rectangle> 
    </Grid> 
</UserControl> 

如果要指定一个暗/光的主题,您可以在设置RequestedTheme控制。