如何根据当前主题应用样式 [英] How to Apply Styles based on Current Theme

查看:49
本文介绍了如何根据当前主题应用样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在尝试根据当前系统主题将样式应用于普通按钮.我为按钮创建了一种新样式,并希望针对每个系统主题定义一个样式(经典,luna.*,aero,aero2,aero2lite).

我已经创建了一个名为"themes \ generic.xaml"的文件,包含样式:

< Style x:Key ="LeftNavigationArrow"; TargetType ="Button" ..</Style>
< Style x:Key =" RightNavigationArrow" TargetType ="Button" ..</Style> 

在主窗口中,应用此主题:

<按钮样式=" {StaticResource LeftNavigationArrow}宽度="24".高度="24"/.
< Button样式=" {{StaticResource RightNavigationArrow}"宽度="24".高度="24"/> 

这不起作用,因为样式"LeftNavigationArrow"和"RightNavigationArrow"找不到.

我的AssemblyInfo.cs文件包含什么似乎无关紧要

 [程序集:ThemeInfo(
    ResourceDictionaryLocation.SourceAssembly,//特定于主题的资源字典所在的位置
    //(如果在页面中找不到资源,
    //或应用程序资源字典)
    ResourceDictionaryLocation.SourceAssembly//通用资源字典所在的位置
    //(如果在页面中找不到资源,
    //应用或任何特定主题的资源词典)
)]

如果我将应用程序资源设置为指向"theme \ generic.xaml",我当然可以使它工作,但是这违背了我正在尝试做的事情-让WPF主题引擎找到正确的样式基于当前系统主题的我(稍后我将创建 Aero和Luna的皮肤,Win8的Aero2).我阅读过的所有其他有关如何获取当前系统主题名称的文章都看起来很hack(使用uxtheme.dll,请尝试使用System.Windows.Forms命名空间,但不会返回名称" ; 的 WPF中使用的主题).因此,看起来甚至连一段启动代码都无法获得主题并根据该主题设置应用程序资源.

由于WPF的理念是皮肤,而不是创建新控件,所以我想知道我正在做的事情是否可行,或者我是否确实需要定义一个从Button派生的新控件. /p>

最好的问候
杰森.

解决方案

请在下面查看这些链接,以查看它们是否有帮助:

http://stackoverflow .com/questions/541468/如何将样式匹配到当前主题wpf

http://stackoverflow.com/questions/1770935/wpf-style-basedon-current > http://stackoverflow.com/questions/10690318/how-to-apply-主题特定样式


Hello,

I'm trying to apply a style to a normal button, based on the current system theme. I have created a new style for a button and would like to define a style per system theme (classic, luna.*, aero, aero2, aero2lite).

I have created a file called "themes\generic.xaml" that contains the style:

<Style x:Key="LeftNavigationArrow" TargetType="Button"> .. </Style>
<Style x:Key="RightNavigationArrow" TargetType="Button"> .. </Style>

In my main window, I apply this theme:

<Button Style="{StaticResource LeftNavigationArrow}" Width="24" Height="24"/>
<Button Style="{StaticResource RightNavigationArrow}" Width="24" Height="24"/>

This doesn't work, because the style "LeftNavigationArrow" and "RightNavigationArrow" can't be found.

It doesn't appear to matter what my AssemblyInfo.cs file contains:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.SourceAssembly, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

Of course I can get it to work if I set the Application resource to point to "theme\generic.xaml", but this defeats what I'm trying to do - let the WPF theme engine find the correct style for me based on the current system theme (later I'll create skins for Aero and Luna, Aero2 for Win8). All other posts that I've read on how to get the name of the current system theme looks like a hack (use uxtheme.dll, try to use the System.Windows.Forms namespace, but that doesn't return the "name" of the theme as used in WPF). So it looks like even a piece of startup code to get the theme and set the application resource based on that will be unreliable.

As the philosophy of WPF is to skin, instead of creating a new control, I'm wondering if what I'm doing is even possible, or if I really do need to define a new control that derives from Button.

Best Regards
Jason.

解决方案

Hi,

Please check -out these links below to see if them help:

http://stackoverflow.com/questions/541468/how-do-i-match-my-controls-styles-to-the-current-theme-wpf

http://stackoverflow.com/questions/1770935/wpf-style-basedon-currenthttp://stackoverflow.com/questions/10690318/how-to-apply-theme-specific-styles


这篇关于如何根据当前主题应用样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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