问题
I created 2 ResourceDictionary
/theme files in Themes folder named Light.xaml
and Dark.xaml
.
Added SolidColorBrush
with name BgColor
in both files :
<SolidColorBrush x:Name="BgColor" Color="Silver" />
// in Light.xaml
<SolidColorBrush x:Name="BgColor" Color="WhiteSmoke" />
// in Dark.xaml
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>
Since this is Library Project
, there's no App.xaml
on my project.
So how to link this ThemeDictionaries
so I can use it in my UserControl
in Library Project
?
回答1:
So how to link this ThemeDictionaries so I can use it in my UserControl?
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="Dark" Source="ms-appx:///MyThemeLibrary/Themes/Light.xaml"/>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
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>
来源:https://stackoverflow.com/questions/39327157/linking-themedictionaries-in-library-project