如何设置按钮背景图像主题是黑色还是白色

时间:2011-08-20 13:21:08

标签: windows-phone-7

我使用下面的代码使用带有白色图像的png文件设置按钮背景图像。

ImageBrush brush = new ImageBrush();    

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

btnDel.Background = brush;

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

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

2 个答案:

答案 0 :(得分:2)

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

本文介绍如何检测手机上的所选主题(黑暗或白色)以及如何使用它来更改控件的背景。在您使用图片的情况下,我建议您创建第二张图片并根据手机上所选主题加载正确的图片。

希望这有帮助! :)

答案 1 :(得分: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}" />