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

How to save Status bar background colour in Local settings

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

有什么建议吗?

你可以这样做:

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 才能使背景颜色生效。

从 .

到十六进制代码的颜色转换代码