How to store App settings in Xamarin.Forms

后端 未结 4 2078
攒了一身酷
攒了一身酷 2020-12-05 09:59

How can we store and retrieve App settings as key,value pair in Xamarin.Forms? Like when the app is closed we can store the user preferences and on restarting of the App we

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 10:24

    You can use the preferences included with Xamarin.Essential.

            /* You need to using Xamarin.Essentials
            If you do not have Xamarin.Essentials installed,
            install it from (NuGet Package Manager)
            */
    
            // Add a reference to Xamarin.Essentials in your class
            using Xamarin.Essentials;
    
            // To save a value for a given key in preferences
            Preferences.Set("my_key", "my_value");
    
            // To retrieve a value from preferences or a default if not set
            var myValue = Preferences.Get("my_key", "default_value");
    
            // To check if a given key exists in preferences
            bool hasKey = Preferences.ContainsKey("my_key");
    
            // To remove the key from preferences
            Preferences.Remove("my_key");
    
            // To remove all preferences
            Preferences.Clear();
    

    Have a look at this link: https://docs.microsoft.com/en-us/xamarin/essentials/preferences?tabs=android

提交回复
热议问题