在图书馆项目中链接 ThemeDictionaries [英] Linking ThemeDictionaries in Library Project
问题描述
我在名为 Light.xaml
和 Dark.xaml
的 Themes 文件夹中创建了 2 个 ResourceDictionary
/theme 文件.
I created 2 ResourceDictionary
/theme files in Themes folder named Light.xaml
and Dark.xaml
.
在两个文件中添加了名称为 BgColor
的 SolidColorBrush
:
Added SolidColorBrush
with name BgColor
in both files :
//在 Light.xaml 中
//在 Dark.xaml 中
在 Application
类型的项目中,我可以在 App.xaml
中添加以下 XAML 代码,以便我可以在我的 UserControl
中引用此资源:
In Application
type project, I can add below XAML code in App.xaml
so I can reference this resource in my UserControl
:
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Dark" Source="Themes/Dark.xaml" />
<ResourceDictionary x:Key="Light" Source="Themes/Light.xaml"/>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
由于这是 Library Project
,我的项目中没有 App.xaml
.
Since this is Library Project
, there's no App.xaml
on my project.
那么如何链接这个ThemeDictionaries
以便我可以在Library Project
的UserControl
中使用它?
So how to link this ThemeDictionaries
so I can use it in my UserControl
in Library Project
?
推荐答案
那么如何链接这个 ThemeDictionaries 以便我可以在我的 UserControl 中使用它?
So how to link this ThemeDictionaries so I can use it in my UserControl?
您可以在 App.xaml
中引用资源字典,如下所示:
You can reference the resource dictionary in your App.xaml
like below:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Dark" Source="ms-appx:///MyThemeLibrary/Themes/Dark.xaml"/>
<ResourceDictionary x:Key="Light" Source="ms-appx:///MyThemeLibrary/Themes/Light.xaml"/>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
MyThemeLibrary
是类库的引用名称.之后,您可以使用您在 Dark.xaml
和 Light.xaml
中定义的主题,如下所示:
MyThemeLibrary
is the reference name of your class library. After that you can use the theme that you defined in Dark.xaml
and Light.xaml
like below:
<UserControl
...
d:DesignHeight="300"
d:DesignWidth="400">
<Grid Background="{ThemeResource BgColor}">
</Grid>
</UserControl>
这篇关于在图书馆项目中链接 ThemeDictionaries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!