xamarin形成共享偏好

时间:2015-03-30 09:47:32

标签: xamarin xamarin.forms

以下代码将写入共享项目。

导入的包

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表单。?

1 个答案:

答案 0 :(得分:5)

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