Xamarin.Forms 共享偏好

Xamarin.Forms shared preferences

以下代码写在共享项目中。


// 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);

当 运行 代码出现以下错误时:

'Android.Preferences.PreferenceManager' does not contain a definition for 'GetSharedPreferences'

在 Xamarin.Forms 中使用 SharedPreferences 的正确方法是什么?

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this); 
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("username", name);
editor.PutString("password", password);
editor.Apply();