我正在使用WP7,我想创建一个自定义画笔(作为本地资源),使用不同颜色的暗色和浅色主题(对于instace,红色如果主题是黑色和蓝色,如果它是白色)。
我该怎么做?
谢谢!
答案 0 :(得分:5)
集成/系统画笔不会根据主题更改其属性,根据当前主题包含不同的画笔集。您可以在%programfiles%\Microsoft SDKs\Windows Phone\v7.1\Design
我写了一个custom ResourceDictionary
以完全相同的方式实现主题支持:根据明/暗主题加载适当的主题词典。
以下是使用它的示例。它适用于Visual Studio设计器和Blend,但不支持Blend中的白色主题预览,因为Blend以无法再现的方式加载资源。
<Application.Resources>
<custom:ThemeResourceDictionary>
<custom:ThemeResourceDictionary.LightResources>
<ResourceDictionary Source="/ThemeManagement;component/Resources/Light.xaml" />
</custom:ThemeResourceDictionary.LightResources>
<custom:ThemeResourceDictionary.DarkResources>
<ResourceDictionary Source="/ThemeManagement;component/Resources/Dark.xaml" />
</custom:ThemeResourceDictionary.DarkResources>
</custom:ThemeResourceDictionary>
</Application.Resources>
上面的代码从两个不同的文件加载资源,但它们可以像任何其他ResourceDictionary一样容易地内联声明。
ThemeResourceDictionary
的来源可以在我的原始博客文章中找到,但如果我的博客崩溃,它也会在a different Stack Overflow question上。
答案 1 :(得分:2)
您必须自己从代码中管理应用于元素的画笔。目前,我发现这是调整到不同PhoneBackgroundColor的唯一方法。
例如:
在xaml
<TextBlock Text="Some text" Foreground="{Binding VariableTextColor}"/>
代码
var backgroundColor = (System.Windows.Media.Color)Application.Current.Resources["PhoneBackgroundColor"];
if(backgroundColor == "#FF000000") //Dark theme selected
VariableTextColor = RedBrush;
else
VariableTextColor = WhiteBrush;
使用PhoneDarkThemeVisibility资源的另一个方法:
/// <summary>
/// Determines if the application is running in the dark theme
/// </summary>
private bool IsDarkTheme
{
get
{
if (IsDesignMode)
{
return true;
}
else
{
return (Visibility)Application.Current
.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible;
}
}
}
答案 2 :(得分:0)
Richard Szalay描述的方法在我的情况下不起作用,因为我在UserControl中需要它,或者因为其他东西,但它确实接近答案。我的项目是Silverlight Windows Phone 8.1项目,我正在使用带有最新更新的Visual Studio 2013。可能这就是问题所在。这对我的案例有帮助
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Application;component/Controls/NumberKeyboard.Dark.xaml" />
<windows:ThemeSelector>
<windows:ThemeSelector.Dark>
<ResourceDictionary Source="/Application;component/Controls/NumberKeyboard.Dark.xaml" />
</windows:ThemeSelector.Dark>
<windows:ThemeSelector.Light>
<ResourceDictionary Source="/Application;component/Controls/NumberKeyboard.Light.xaml" />
</windows:ThemeSelector.Light>
</windows:ThemeSelector>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="KeyboardButton" TargetType="controls:SimpleButton">
<Setter Property="FontSize" Value="45" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Style>
<Style x:Key="NumberButton" TargetType="controls:SimpleButton" BasedOn="{StaticResource KeyboardButton}">
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" />
<Setter Property="Background" Value="{StaticResource ButtonBrush}" />
</Style>
<Style x:Key="ControlButton" TargetType="controls:SimpleButton" BasedOn="{StaticResource KeyboardButton}">
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}" />
<Setter Property="Background" Value="{StaticResource ButtonBrush}" />
</Style>
<Style x:Key="ActionButton" TargetType="controls:SimpleButton" BasedOn="{StaticResource KeyboardButton}">
<Setter Property="Foreground" Value="{StaticResource PhoneBackgroundBrush}" />
<Setter Property="Background" Value="{StaticResource PhoneForegroundBrush}" />
</Style>
</ResourceDictionary>
</UserControl.Resources>
关键行<ResourceDictionary Source="/Application;component/Controls/NumberKeyboard.Dark.xaml" />
正好在<windows:ThemeSelector>
之前。该行是VS设计人员正确显示所有内容所必需的,而ThemeSelector会根据当前主题暗或亮来覆盖运行时的样式。