Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms shared preferences

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 ?

like image 527
Bhaumik Shah Avatar asked Mar 30 '15 09:03

Bhaumik Shah


1 Answers

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this); 
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("username", name);
editor.PutString("password", password);
editor.Apply();
like image 94
jinal Avatar answered Oct 20 '22 17:10

jinal