Linking ThemeDictionaries in Library Project

别来无恙 提交于 2020-01-05 05:18:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!