The followig code is written in the Shared Project.
// importing Packages
using Android.Content;
using Android.Preferences;
// storing the values as SharedPreferences
ISharedPreferences pref = PreferenceManager.GetSharedPreferences("UserInfo", FileCreationMode.Private);
ISharedPreferencesEditor edit = pref.Edit();
edit.PutString("Username", username.Text.Trim());
edit.PutString("Password", password.Text.Trim());
edit.Apply();
// retrieving the values
ISharedPreferences pref = PreferenceManager.GetSharedPreferences("UserInfo", FileCreationMode.Private);
string userName = pref.GetString("Username", String.Empty);
string password = pref.GetString("Password", String.Empty);
When running the code we get that following error:
'Android.Preferences.PreferenceManager' does not contain a definition for 'GetSharedPreferences'
What's the correct way to use SharedPreferences
in Xamarin.Forms ?
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("username", name);
editor.PutString("password", password);
editor.Apply();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With