当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to get Windows Phone 7 Theme Colour with XNA

问题描述:

I've been trying to use the guide available at http://geekswithblogs.net/mikebmcl/archive/2010/09/16/using-wp7-themes-in-your-xna-game.aspx but I cannot find the Application name, nor do I seem to be able to find a replacement for SolidColorBrush.

Unfortunately there is no library or easy to use code on the net to programmitcally get the tile colour in XNA on windows phone, even though its simple with Silverlight.

Any ideas how to go about this?

网友答案:

You can get the theme (dark/light) on the phone in a shorter way (works for XNA too):

Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];

if(darkBackgroundVisibility == Visibility.Visible)
    //Theme is Dark
else
    //Theme is Light

To get the AccentColor, you need a but more code (I got it from this article on MSDN: How to: Apply Theme Resources for Windows Phone). I shortened the code from the switch-statement for readability and put it in a method. I also tested this in an XNA app and this works fine! :)

var currentAccentColorHex = (System.Windows.Media.Color)Application.Current.Resources["PhoneAccentColor"];
string currentAccentColor = ColorNameFromHex(currentAccentColorHex);

private string ColorNameFromHex(System.Windows.Media.Color hexColor)
{
    switch(hexColor.ToString())
    {
        case "#FF1BA1E2": return "Blue";
        case "#FFA05000": return "Brown";
        case "#FF339933": return "Green";
        case "#FFE671B8": return "Pink";
        case "#FFA200FF": return "Purple";
        case "#FFE51400": return "Red";
        case "#FF00ABA9": return "Teal";
        case "#FF8CBF26":
        case "#FFA2C139": return "Lime";
        case "#FFFF0097":
        case "#FFD80073": return "Magenta";
        case "#FFF09609": return "Mango";
        default: return "custom eleventh color"; //Manufacturer color
    }
}

Instead of returning a string containin 'Red' you could return a 'real' Color. For that you'll have to change return type of the method and the value.

Hope this helps!

网友答案:

You can get the current theme from the Resources for example getting the background color like this. In an App you could check this in the Application_Launching as well as Application_Activated to see if the theme changed while the App was in the background.

I'm pretty sure you can do a similar thing in an XNA game:

public enum PhoneTheme
    {
        Light,
        Dark
    };

public static PhoneTheme CurrentTheme { get; private set; }

Following in your activated/startup code:

string theme = Resources["PhoneBackgroundColor"].ToString();

CurrentTheme = theme == "#FF000000"
                        ? PhoneTheme.Dark
                        : PhoneTheme.Light;
分享给朋友:
您可能感兴趣的文章:
随机阅读: