我们如何在xamarin.forms中的本地数据库中存储值,即用户名和密码。
答案 0 :(得分:1)
我在使用依赖注入之前已经这样做了。
首先创建一个将在Android和iOS项目中实现的界面
public interface UserInterface
{
void saveUserName(string userName);
string loadUserName();
}
然后在android和iOS项目中实现该接口
机器人:
[assembly: Xamarin.Forms.Dependency (typeof (ChessUser_Android))]
public class ChessUser_Android : Java.Lang.Object, UserInterface {
public void saveUserName(string userName){
var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
var prefEditor = prefs.Edit();
//save user's properties for a specific key
prefEditor.PutString("user_name",userName);
prefEditor.Commit();
}
public string loadUserName(){
var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
return prefs.GetString("user_name",null);
}
}
的iOS
[assembly: Xamarin.Forms.Dependency (typeof (ChessUser_iOS))]
public class ChessUser_iOS : UserInterface {
public void saveUserName(string userName){
var prefs = NSUserDefaults.StandardUserDefaults;
//save user's properties for a specific keys
prefs.SetValueForKey (new NSString (userName), new NSString ("user_name"));
}
public string loadUserName(){
var prefs = NSUserDefaults.StandardUserDefaults;
return prefs.StringForKey ("username");
}
}
当我想保存或加载数据时,即用户名和密码
DependencyService.Get<UserInterface>().loadUserName();
DependencyService.Get<UserInterface>().saveUserName("name");
答案 1 :(得分:1)
虽然其他答案中显示的完整SQLite支持都很棒,但如果您只需要简单的键值存储(例如:用户名和密码),这里就是另一种选择。它抽象NSUserDefaults(iOS)和SharedPreferences(Android)。
共享项目
public interface ISimpleStorage
{
void Set(string key, string value);
string Get(string key);
void Delete(string key);
}
iOS项目
[assembly: Dependency(typeof(SimpleStorage))]
namespace MyApp.iOS
{
public class SimpleStorage : ISimpleStorage
{
public void Set (string key, string value)
{
NSString newKey = new NSString (key);
NSString newValue = new NSString (value);
NSUserDefaults.StandardUserDefaults.SetValueForKey (newValue, newKey);
}
public string Get (string key)
{
NSString newKey = new NSString (key);
NSString value = (NSString)NSUserDefaults.StandardUserDefaults.ValueForKey (newKey);
return value != null ? value.ToString () : null;
}
public void Delete (string key)
{
NSString newKey = new NSString (key);
NSUserDefaults.StandardUserDefaults.RemoveObject (newKey);
}
}
}
<强>的Android 强>
[assembly: Dependency(typeof(SimpleStorage))]
namespace MyApp.Android
{
public class SimpleStorage : ISimpleStorage
{
public void Set (string key, string value)
{
var prefs = Forms.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
var prefEditor = prefs.Edit();
prefEditor.PutString (key, value);
prefEditor.Commit();
}
public string Get (string key)
{
var prefs = Forms.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
return prefs.GetString(key, null);
}
public void Delete (string key)
{
var prefs = Forms.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
var prefEditor = prefs.Edit ();
prefEditor.Remove (key);
prefEditor.Commit();
}
}
}
然后我在我的Shared项目中有一个helper类,如下所示:
public static class Device
{
public static ISimpleStorage Storage { get { return DependencyService.Get<ISimpleStorage> ();}}
}
然后我可以在我的共享代码中使用它,如下所示:
var username = Device.Storage.Get ("username");
Device.Storage.Set ("username", textEntry.Text);
Device.Storage.Delete ("username");
答案 2 :(得分:0)
Xamarin有一个guide用于将SQLite与Xamarin Forms一起使用