2011-08-20 105 views
0

我使用下面的代码来设置按钮背景图像使用PNG文件与白色图像。如何设置按钮背景图像任一主题是黑色或白色

ImageBrush brush = new ImageBrush();  

brush.ImageSource = new BitmapImage(new Uri(@"/Images/delete.png", UriKind.Relative));  

btnDel.Background = brush; 

png文件有一个白色的图像。在黑暗主题中,我可以看到png文件中的白色图像。但是,当我将主题更改为光明时,我再也看不到白色图像。

我是否需要检测用户设置的主题,然后使用另一个带有黑色图像的png文件?

回答

1

我写了一个blog post回来,其中包括一个自定义的ResourceDictionary实现,支持根据主题在两个ResourceDictionaries之间进行交换。

它可以与Visual Studio设计器(Cider)和Blend一起使用,但在Blend中使用该预览机制时不会交换到光源。不幸的是,由于Blend如何处理资源,它看起来在可预见的将来会保持这种状态。

您可以阅读原帖的其他信息,但我会在这里包含的代码:

namespace ThemeManagement { 
    /// <summary> 
    /// Provides automatic selection of resources based on the current theme 
    /// </summary> 
    public class ThemeResourceDictionary : ResourceDictionary 
    { 
     private ResourceDictionary lightResources; 
     private ResourceDictionary darkResources; 

     /// <summary> 
     /// Gets or sets the <see cref="ResourceDictioary"/> to use 
     /// when in the "light" theme 
     /// </summary> 
     public ResourceDictionary LightResources 
     { 
      get { return lightResources; } 
      set 
      { 
       lightResources = value; 

       if (!IsDarkTheme && value != null) 
       { 
        MergedDictionaries.Add(value); 
       } 
      } 
     } 

     /// <summary> 
     /// Gets or sets the <see cref="ResourceDictioary"/> to use 
     /// when in the "dark" theme 
     /// </summary> 
     public ResourceDictionary DarkResources 
     { 
      get { return darkResources; } 
      set 
      { 
       darkResources = value; 

       if (IsDarkTheme && value != null) 
       { 
        MergedDictionaries.Add(value); 
       } 
      } 
     } 

     /// <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; 
       } 
      } 
     } 

     /// <summary> 
     /// Determines if the application is being run by a design tool 
     /// </summary> 
     private bool IsDesignMode 
     { 
      get 
      { 
       // VisualStudio sometimes returns false for DesignMode, 
       // DesignTool is our backup 
       return DesignerProperties.GetIsInDesignMode(this) || 
        DesignerProperties.IsInDesignTool; 
      } 
     } 
    } 
} 

然后,您可以这样定义光/暗特定的资源(或者你可以把他们在自己的资源XAML文件):

<Application.Resources> 
    <custom:ThemeResourceDictionary> 
     <custom:ThemeResourceDictionary.LightResources> 
      <ResourceDictionary> 
       <BitmapImage x:Key="ThemedImage" 
        UriSource="/ThemeManagement;component/Content/ImageLight.png" /> 
      </ResourceDictionary> 
     </custom:ThemeResourceDictionary.LightResources> 
     <custom:ThemeResourceDictionary.DarkResources> 
      <ResourceDictionary> 
       <BitmapImage x:Key="ThemedImage" 
        UriSource="/ThemeManagement;component/Content/ImageDark.png" /> 
      </ResourceDictionary> 
     </custom:ThemeResourceDictionary.DarkResources> 
    </custom:ThemeResourceDictionary> 
</Application.Resources> 

然后,您可以参考它们在页面上是这样的:

<Image Source="{StaticResource ThemedImage}" /> 
+0

谢谢。有用。 – MilkBottle

2

http://www.mendzapp.com/archives/196

本文介绍如何检测手机(黑色或白色),以及如何使用此选项可以更改控件的背景下所选择的主题。在你使用图像的情况下,我建议创建第二个图像并根据手机上选定的主题加载正确的图像。

希望这会有所帮助! :)