Silverlight 如何创建既支持黑暗主题又支持光明主题的画笔?

Silverlight 如何创建既支持黑暗主题又支持光明主题的画笔?,silverlight,windows-phone-7,themes,Silverlight,Windows Phone 7,Themes,我正在使用WP7,我想创建一个自定义画笔(作为本地资源),该画笔对深色和浅色主题使用不同的颜色(对于Instance,如果主题为黑色,则为红色;如果主题为白色,则为蓝色) 我该怎么做 谢谢 您必须自己管理从代码应用到元素的笔刷。目前,我发现这是适应不同电话背景颜色的唯一方法 例如: 在xaml中 <TextBlock Text="Some text" Foreground="{Binding VariableTextColor}"/> 另一种使用资源的方法: /// <

我正在使用WP7,我想创建一个自定义画笔(作为本地资源),该画笔对深色和浅色主题使用不同的颜色(对于Instance,如果主题为黑色,则为红色;如果主题为白色,则为蓝色)

我该怎么做


谢谢

您必须自己管理从代码应用到元素的笔刷。目前,我发现这是适应不同电话背景颜色的唯一方法

例如:

在xaml中

<TextBlock Text="Some text" Foreground="{Binding VariableTextColor}"/>
另一种使用资源的方法:

    /// <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;
            }
        }
    } 
//
///确定应用程序是否在黑暗主题中运行
/// 
私人布尔伊斯达克酒店
{
得到
{
if(IsDesignMode)
{
返回true;
}
其他的
{
return(可见性)Application.Current
.Resources[“phonedarkthemeviability”]==可见性.Visible;
}
}
} 

集成/系统笔刷不会根据主题更改其属性,而是根据当前主题包含一组不同的笔刷。您可以在
%programfiles%\Microsoft SDK\Windows Phone\v7.1\Design

我编写了一个以完全相同的方式实现主题支持的工具:根据亮/暗主题加载适当的主题词典

下面是一个使用它的示例。它在Visual Studio designer和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>

以防我的博客崩溃。

理查德·萨莱(Richard Szalay)所描述的方法在我的案例中不起作用,要么是因为我在用户控件中需要它,要么是因为其他原因,但它确实接近于答案。我的项目是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>


关键行是
前面的
。该行是VS designer正确显示所有内容所必需的,并且该选择器在运行时根据当前主题深色或浅色覆盖样式。

使用,而不是压缩背景色。您的代码与特定于供应商的背景色不兼容!另外,我认为在中使用两个不同样式的资源字典比使用随机C#更好!说得好。非常感谢。我不知道有特定于供应商的背景色。我的HTC只有黑暗和光明。新的HTC泰坦有一个黑暗主题的白色背景。(不要问我为什么。)我不喜欢这个解决方案,我想创建一个自定义笔刷,用户体验设计师可以自由使用,而无需任何代码更改。这应该和其他集成画笔一样工作,在我的应用程序上没有任何代码的情况下改变颜色。克劳斯为什么不呢?这是因为Metro风格的实施吗?可能是非常有趣的重复,我喜欢你的解决方案,但对我的UX设计师来说还不够好,因为她在Blend中看不到的东西并不存在;)它将以混合显示,只是在她使用主题预览功能时不会变为白色。在我看来,你不会比这更接近了——我在Reflector中浏览了一半的混合源,试图找到一种正确的方法。这个想法很好,但似乎不起作用,或者我做错了什么。它在运行时工作,但设计师无法从黑暗和光明文件中找到任何键。@alex.49.98您可能没有做错任何事情。此解决方案基于Winodws Phone 7.1,因此我不确定它是否转换为最新的WP8.1应用程序模型。与您的示例至少有一个差异,我在开始时没有注意到。我在UserControl.Resources中拥有所有这些,但幸运的是,我找到了一个解决方案。
   <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>