简体   繁体   中英

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. What I did is creating staticressources (colors) in 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. I heard about "App.Current.Properties["Id"] =..." but I can't figure it out. If someone know how to do i'll be happy to know. Thank you.

You can use Xamarin.Essentials: Preferences

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.

In MainPage.xaml at the top

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;

 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.

StaticResource is set and DynamicResource you can change

App.xaml.cs

    <Application.Resources>

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

</Application.Resources>

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.

 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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