Xamarin.Forms学习历程(七)——用户偏好设置存储

经常会遇到要存储一下用户账号密码之类的,让用户下次登录时不需要重新输入账号密码,直接进入主界面。Xamarin.Forms里没有自己的解决方案,还是得调用iOS和Android原生的API才可以实现。
在Xamarin.Forms里怎么实现呢,整体思路是将iOS和Android原生的API分装成一个公共的接口,然后让Xamarin调这个接口,若在Android端则去执行Android端的API,在iOS端则执行iOS端的代码。好了,下面我们看具体实现吧。

1、封装iOS端API##

以下代码在iOS项目里写,单独写成一个类

namespace XamarinJaguarFund.iOS
{
    public class UserPreferencesiOS : IUserPreferences
    {
        public UserPreferencesiOS()
        {
        }

        public void SetString(string key, string value)
        {
            NSUserDefaults.StandardUserDefaults.SetString(value,key);
        }

        public string GetString(string key)
        {
            return NSUserDefaults.StandardUserDefaults.StringForKey(key);
    
        }

        public void DeleteString(string key)
        {
            NSUserDefaults.StandardUserDefaults.RemoveObject(key);
        }
    }
}

然后设置AppDelegate.cs

namespace XamarinJaguarFund.iOS
{
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            OxyPlot.Xamarin.Forms.Platform.iOS.PlotViewRenderer.Init();

            App.Init(new UserPreferencesiOS());
            App myApp = new App();

            LoadApplication(myApp);

            return base.FinishedLaunching(app, options);
        }
    }
}

完成这两步,iOS部分就算封装完毕了。

2、封装Android端API##

以下代码在Android项目写,单独写个类。

namespace XamarinJaguarFund.Droid
{
    public class UserPreferencesAndroid : IUserPreferences
    {
        public UserPreferencesAndroid()
        {
        }


        public void SetString(string key, string value)
        {
            var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);
            var prefsEditor = prefs.Edit();

            prefsEditor.PutString(key, value);
            prefsEditor.Commit();
        }

        public string GetString(string key)
        {
            var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);
            return prefs.GetString(key,"");
    
        }

        public void DeleteString(string key)
        { 
            var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);
            prefs.Edit().Remove(key).Commit();
        }
    }
}

然后设置MainActivity.cs

namespace XamarinJaguarFund.Droid
{
    [Activity(Label = "XamarinJaguarFund.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
        
            App.Init(new UserPreferencesAndroid());
            App myApp = new App();
            LoadApplication(myApp);
        }
    }
}

完成这两步,Android部分就算封装完毕了。

3、公共部分设置##

在Xamarin项目里单独写个类

namespace XamarinJaguarFund
{
    public interface IUserPreferences
    {
        void SetString(string key, string value);
        string GetString(string key);
        void DeleteString(string key);
    }
}

在app.cs初始化以下

public partial class App : Application
    {

        public static IUserPreferences UserPreferences { get; private set; }

        public static void Init(IUserPreferences userPreferencesImpl)
        {
            App.UserPreferences = userPreferencesImpl;
        }
    }

4、使用##

设置值

App.UserPreferences.SetString("token", userObject.token);
App.UserPreferences.SetString("displayName", userObject.displayName);
App.UserPreferences.SetString("username", userObject.username);

查询值

customerName.Text = App.UserPreferences.GetString("displayName");
displayName.Text = App.UserPreferences.GetString("username");

删除值

App.UserPreferences.DeleteString("token");              App.UserPreferences.DeleteString("displayName");
App.UserPreferences.DeleteString("username");

如果要修改某个值,只需使用设置值的方法,值会覆盖。

结语#

有点小复杂,不过理解其核心思想就可以理解了。如有错误之处还望大神给予指点。

你可能感兴趣的:(Xamarin.Forms学习历程(七)——用户偏好设置存储)