在 Xamarin.Forms 中编写特定于设备平台的代码 [英] Write device platform specific code in Xamarin.Forms

查看:19
本文介绍了在 Xamarin.Forms 中编写特定于设备平台的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Xamarin.Forms.ContentPage 类结构

public class MyPage : ContentPage
{
    public MyPage()
    {
        //do work to initialize MyPage 
    }

    public void LogIn(object sender, EventArgs eventArgs)
    {
        bool isAuthenticated = false;
        string accessToken = string.Empty;

        //do work to use authentication API to validate users

        if(isAuthenticated)
        {
            //I would to write device specific code to write to the access token to the device
            //Example of saving the access token to iOS device
            NSUserDefaults.StandardUserDefaults.SetString(accessToken, "AccessToken");

            //Example of saving the access token to Android device
            var prefs = Application.Context.GetSharedPreferences("MySharedPrefs", FileCreationMode.Private);
            var prefsEditor = prefs.Edit();

            prefEditor.PutString("AccessToken", accessToken);
            prefEditor.Commit();
        }
    }
}

我想在 MyPage LogIn 方法中编写特定于平台的代码,以根据他们使用我的应用程序的设备操作系统保存访问令牌.

I would like to write platform specific code in the MyPage LogIn method to save the access token based on which device OS they are using my application on.

当用户在他们的设备上使用我的应用程序时,我如何只运行设备特定的代码?

How do I only run device specific code when the user uses my application on their device?

推荐答案

这是一个很容易通过依赖注入解决的场景.

This is a scenario which is easily resolved with dependency injection.

在您的共享或 PCL 代码上有一个包含所需方法的接口,例如:

Have a interface with the desired methods on your shared or PCL code, like:

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

在该接口的 App 类上有一个属性:

Have a property on your App class of that interface:

public class App 
{
    public static IUserPreferences UserPreferences { get; private set; }

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

    (...)
}

在您的目标项目上创建特定于平台的实现:

Create platform-specific implementations on your target projects:

iOS:

public class iOSUserPreferences : IUserPreferences 
{
    public void SetString(string key, string value)
    {
        NSUserDefaults.StandardUserDefaults.SetString(key, value);
    }

    public string GetString(string key)
    {
        (...)
    }
}

安卓:

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

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

    public string GetString(string key)
    {
        (...)
    }
}

然后在每个特定于平台的项目上创建 IUserPreferences 的实现并使用 App.Init(new iOSUserPrefernces())App.Init 设置它(new AndroidUserPrefernces()) 方法.

Then on each platform-specific project create an implementation of IUserPreferences and set it using either App.Init(new iOSUserPrefernces()) and App.Init(new AndroidUserPrefernces()) methods.

最后,您可以将代码更改为:

Finally, you could change your code to:

public class MyPage : ContentPage
{
    public MyPage()
    {
        //do work to initialize MyPage 
    }

    public void LogIn(object sender, EventArgs eventArgs)
    {
        bool isAuthenticated = false;
        string accessToken = string.Empty;

        //do work to use authentication API to validate users

        if(isAuthenticated)
        {
            App.UserPreferences.SetString("AccessToken", accessToken);
        }
    }
}

这篇关于在 Xamarin.Forms 中编写特定于设备平台的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆