以下代码将写入共享项目。
导入的包
using Android.Content;
using Android.Preferences;
将值存储到共享首选项
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();
用于检索值
ISharedPreferences pref = PreferenceManager.GetSharedPreferences("UserInfo", FileCreationMode.Private);
string userName = pref.GetString("Username", String.Empty);
string password = pref.GetString("Password", String.Empty);
我们遇到以下错误
' Android.Preferences.PreferenceManager'不包含' GetSharedPreferences'
的定义如何将共享首选项用于Xamarin表单。?
答案 0 :(得分:5)
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("username", name);
editor.PutString("password", password);
editor.Apply();