如何在本地设置中保存状态栏背景颜色

时间:2015-01-05 07:12:34

标签: c# windows-phone-8.1

我已经让用户可以自由更改背景颜色并将其保存在本地设置中。但我对如何保存状态栏颜色设置感到困惑,因为我想让用户自由选择4-5种颜色,可以更改状态栏的背景。

有任何建议吗?

1 个答案:

答案 0 :(得分:0)

您可以这样做:

public static void SaveStatusBarColor()
{
    // get status bar color
    var statusBar = StatusBar.GetForCurrentView();
    var color = statusBar.BackgroundColor;

    // convert color to hex string
    var hexCode = color.HasValue ? color.Value.ToString() : "";

    // store color in local settings
    ApplicationData.Current.LocalSettings.Values["StatusBarColor"] = hexCode;
}

public static void LoadStatusBarColor()
{
    // get color hex string from local settings
    var hexCode = ApplicationData.Current.LocalSettings.Values["StatusBarColor"] as string;
    if (string.IsNullOrEmpty(hexCode))
        return;

    // convert hexcode to color
    var color = new Color();
    color.A = byte.Parse(hexCode.Substring(1, 2), NumberStyles.AllowHexSpecifier);
    color.R = byte.Parse(hexCode.Substring(3, 2), NumberStyles.AllowHexSpecifier);
    color.G = byte.Parse(hexCode.Substring(5, 2), NumberStyles.AllowHexSpecifier);
    color.B = byte.Parse(hexCode.Substring(7, 2), NumberStyles.AllowHexSpecifier);

    // set the status bar color
    var statusBar = StatusBar.GetForCurrentView();
    statusBar.BackgroundColor = color;
}

请记住,StatusBar.BackgroundOpacity最初为0,因此您需要将其设置为1才能使背景颜色生效。

来自https://stackoverflow.com/a/16815300/734040的Hexcode到颜色转换代码。