如何在wpf应用程序中包含和使用Themes [英] How to include and use Themes in wpf application

查看:326
本文介绍了如何在wpf应用程序中包含和使用Themes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Hi,


我必须包括主题我的WPF应用程序,并使用它们的外观和感觉windows(.xaml)和控件。

I have to include themes to my wpf application and use them for look and feel of the windows(.xaml) and controls.


i试图包含主题文件,如下面的代码,但它抛出错误。

i tried to include the theme files like below code but it is throwing error.

 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Themes/BureauBlack.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </Application.Resources>



如何分配主题文件的源路径,该文件将Build操作指定为Resource。还应该有选项在运行时更改主题。

How to assign source path of a theme file which is assign Build action as Resource. And also should have option to change the themes during run time.

推荐答案

抛出了什么错误?

ResourceDictionary Build Action应该是Page(默认情况下)。

ResourceDictionary Build Action should be Page(by default).

你可以使用打包URI以指定ResourceDictionary文件。

You can use Pack URIs to specify ResourceDictionary file.

https://docs.microsoft.com/en-us/dotnet/framework/wpf/app-development/pack-uris-in-wpf

要在运行时刻录主题,您应该使用DynamicResource,DynamicResource在加载期间为属性分配一个Expression对象,但实际上在运行时之前不会查找资源。  ;如果源资源
字典发生更改,它将更新目标。

To chage the themes during run time, You should use DynamicResource, a DynamicResource assigns an Expression object to the property during loading but does not actually lookup the resource until runtime .  It will update the target if the source resource dictionary is changed.

基于此,您可以通过加载不同的资源词典来更改外观。

 ResourceDictionary skin = Application.LoadComponent(new Uri("xxx.xaml", UriKind.Relative)) as ResourceDictionary;
 Application.Current.Resources.Clear();
 Application.Current.Resources.MergedDictionaries.Clear();
 Application.Current.Resources.MergedDictionaries.Add(skin);

我建议您参考这个好样本:

I suggest you refer this good sample:

https://www.codeproject.com/Articles/19782/Creating-a-Skinned-User-Interface-in-WPF

https://www.codeproject.com/Articles/19782/Creating-a-Skinned-User-Interface-in-WPF

此致,

Bob

Bob


这篇关于如何在wpf应用程序中包含和使用Themes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆