0
我正在創建一個包含自定義控件的UWP庫,並且我想爲我的控件定義一個黑暗和輕盈的主題。我知道我們可以將主題資源字典添加到App.xaml。我們可以在那裏指定一個黑暗/淺色主題,並設置請求的主題屬性。但是因爲我正在創建一個庫,所以我的項目中沒有App.xaml。在圖書館項目中定義一個主題對我來說可行嗎?怎麼樣?如何在UWP庫中定義主題?
我正在創建一個包含自定義控件的UWP庫,並且我想爲我的控件定義一個黑暗和輕盈的主題。我知道我們可以將主題資源字典添加到App.xaml。我們可以在那裏指定一個黑暗/淺色主題,並設置請求的主題屬性。但是因爲我正在創建一個庫,所以我的項目中沒有App.xaml。在圖書館項目中定義一個主題對我來說可行嗎?怎麼樣?如何在UWP庫中定義主題?
您可以通過使用添加>新項...>資源字典從項目菜單選項來創建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
控制。