Dre*_*kes 10 wpf skins themes styles controltemplate
WPF允许控件库为不同的系统主题提供不同的资源字典,基本上允许应用程序匹配操作系统选择的视觉主题(Aero,Luna等).
我想知道我是否可以在我的应用程序中包含多个主题资源字典,并在框架中使用一些现有的主题支持.这应该适用于我自己的主题名称,理想情况下允许用户在运行时更改主题,从而更改应用程序的外观.即使这只是配置设置,它仍然可能很有趣.
ben*_*wey 12
这是我在我的应用程序中使用的支持主题的代码片段.在这个例子中,我有两个主题(默认和经典XP).主题资源分别存储在DefaultTheme.xaml和ClassicTheme.xaml中.
这是我的App.xaml中的默认代码
<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ArtworkResources.xaml" />
<ResourceDictionary Source="DefaultTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="SwooshButton" TargetType="ButtonBase">
<!-- style setters -->
</Style>
<!-- more global styles -->
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
然后在App.xaml的代码后面我有以下方法来允许更改主题.基本上,您所做的是清除资源字典,然后使用新主题重新加载字典.
private Themes _currentTheme = Themes.Default;
public Themes CurrentTheme
{
get { return _currentTheme; }
set { _currentTheme = value; }
}
public void ChangeTheme(Themes theme)
{
if (theme != _currentTheme)
{
_currentTheme = theme;
switch (theme)
{
default:
case Themes.Default:
this.Resources.MergedDictionaries.Clear();
AddResourceDictionary("ArtworkResources.xaml");
AddResourceDictionary("DefaultTheme.xaml");
break;
case Themes.Classic:
this.Resources.MergedDictionaries.Clear();
AddResourceDictionary("ArtworkResources.xaml");
AddResourceDictionary("ClassicTheme.xaml");
break;
}
}
}
void AddResourceDictionary(string source)
{
ResourceDictionary resourceDictionary = Application.LoadComponent(new Uri(source, UriKind.Relative)) as ResourceDictionary;
this.Resources.MergedDictionaries.Add(resourceDictionary);
}
Run Code Online (Sandbox Code Playgroud)
使用此方法时您还需要记住的是,任何使用主题的样式都需要具有动态资源.例如:
<Window Background="{DynamicResource AppBackgroundColor}" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3409 次 |
最近记录: |