简体   繁体   English

如何在关闭时保存背景颜色

[英]How to save background color on close

I'm doing an android game with Xamarin Forms and in this game you can choose if the theme is dark or light.我正在用 Xamarin Forms 做一个 android 游戏,在这个游戏中你可以选择主题是深色还是浅色。 What I did is creating staticressources (colors) in App.xmal.我所做的是在 App.xmal 中创建静态资源(颜色)。

<Application.Resources>

    <Color x:Key="PrincipalColor" >#181818</Color>
    <Color x:Key="PrincipalColorInvert" >#ffffff</Color>

</Application.Resources>

But when the user changes the theme (the staticressources color) and quit the game it doesn't save his preferences.但是当用户更改主题(staticressources 颜色)并退出游戏时,它不会保存他的偏好。 I heard about "App.Current.Properties["Id"] =..." but I can't figure it out.我听说过“App.Current.Properties["Id"] =...”,但我想不通。 If someone know how to do i'll be happy to know.如果有人知道该怎么做,我会很高兴知道。 Thank you.谢谢你。

You can use Xamarin.Essentials: Preferences您可以使用Xamarin.Essentials: Preferences

https://learn.microsoft.com/en-us/xamarin/essentials/preferences?tabs=android https://learn.microsoft.com/en-us/xamarin/essentials/preferences?tabs=android

Save a color in Resources , in the example with a Button set a color Green or Red.Resources中保存颜色,在使用 Button 的示例中,设置颜色为绿色或红色。

In MainPage.xaml at the top在顶部的 MainPage.xaml

BackgroundColor="{DynamicResource defaultBackgroundColor}"

Then the Buttons然后是按钮

  <Button Clicked="Button_Clicked" Text="Green" />

    <Button Clicked="Button_Clicked_1" Text="Red" />

In MainPage.xaml.cs using Xamarin.Essentials;在 MainPage.xaml.cs 中using Xamarin.Essentials;

 public MainPage()
    {
        InitializeComponent();
        App.Current.Resources["defaultButtonBackgroundColor"] = Preferences.Get("defaultButtonBackgroundColor", "Blue");
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        Preferences.Set("BackgroundColor", "Green");
        App.Current.Resources["defaultBackgroundColor"] = Preferences.Get("BackgroundColor", "Blue");
    }

    private void Button_Clicked_1(object sender, EventArgs e)
    {
        Preferences.Set("BackgroundColor", "Red");
        App.Current.Resources["defaultBackgroundColor"] = Preferences.Get("BackgroundColor", "Blue");
    }

Also use it for change TextColor of a Button etc, PricipalColor for example.也可以使用它来更改按钮等的 TextColor,例如 PricipalColor。

StaticResource is set and DynamicResource you can change StaticResource已设置,您可以更改DynamicResource

App.xaml.cs应用程序.xaml.cs

    <Application.Resources>

    <Color x:Key="PrincipalColor" >#181818</Color>

</Application.Resources>

MainPage.xaml.cs MainPage.xaml.cs

   public MainPage()
    {
        InitializeComponent();
        App.Current.Resources["defaultButtonBackgroundColor"] = Preferences.Get("defaultButtonBackgroundColor", "Blue");
        App.Current.Resources["PrincipalColor"] = Preferences.Get("PrincipalColor", "#181818");
    }

Change and PrincipalColor to White or any other color with Button.使用 Button 将 PrincipalColor 更改为白色或任何其他颜色。

 private void Button_Clicked(object sender, EventArgs e)
    {
        Preferences.Set("PrincipalColor", "White");
        App.Current.Resources["PrincipalColor"] = Preferences.Get("PrincipalColor", "#181818");
    }

MainPage.xaml in the first Button is Static and the 2e Dynamic changes TextColor MainPage.xaml 在第一个 Button 是Static和 2e Dynamic改变 TextColor

<Button Clicked="Button_Clicked" Text="Green" TextColor="{StaticResource PrincipalColor}" />

    <Button Clicked="Button_Clicked_1" Text="Red" TextColor="{DynamicResource PrincipalColor}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM