32

我有以下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方法中编写特定于平台的代码,以根据他们在哪个设备操作系统上使用我的应用程序来保存访问令牌。

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

4

5 回答 5

64

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

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

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

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

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

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

    (...)
}

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

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())方法进行设置。

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

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);
        }
    }
}
于 2014-06-16T20:42:03.570 回答
15

Xamarin.Forms 2.3.4 为此引入了一种新方法:

if (Device.RuntimePlatform == Device.Android)
{
    // Android specific code
}
else if (Device.RuntimePlatform == Device.iOS)
{
    // iOS specific code
}
else if (Device.RuntimePlatform == Device.UWP)
{
    // UWP specific code
}

还有其他平台可供选择,您可以Device.在 Visual Studio 中输入,它会显示选项。

于 2020-06-10T00:27:54.170 回答
9

有多种答案,具体取决于您想要实现的目标以及您拥有的项目类型:

Xamarin.Forms在不同的平台上执行不同的代码。
如果您想在不同平台上使用不同的字体大小,请使用此选项:

label.Font = Device.OnPlatform<int> (12, 14, 14);

在共享 (PCL) 项目中执行特定于平台的代码 常见的模式是为此使用 DI(依赖注入)。Xamarin.Forms为此提供了一个简单DependencyService的方法,但可以使用任何你想要的。

在共享(共享资产项目)项目中执行特定于平台的代码 由于代码是按平台编译的,因此您可以将特定于平台的代码包装在同一文件中,#if __PLATFORM__ #endif并将所有代码放在同一个文件中。平台项目应定义__IOS__和。请注意,包含和代码的共享资产项目不适用于 iOS 上的 iOS ,并且具有编译器指令会使您的代码更难阅读和测试。__ANDROID____WINDOWS_PHONE__XamlXamarin.Studio

于 2014-06-16T20:51:30.020 回答
2

这似乎Xamarin.Forms更多的是关于在 PCL 中使用默认值。查看James Montemagno 的 github 存储库以进行跨平台默认设置。

然后只需调用他的静态方法进行设置/检索。nuget 包是Xam.Plugins.Settings.

它可以这样使用:

using Refractored.Xam.Settings;

...

CrossSettings.Current.AddOrUpdateValue("AccessToken", accessToken);
var value = CrossSettings.Current.GetValueOrDefault<string>("AccessToken");
于 2014-06-16T20:33:00.227 回答
2

Xamarin.Forms 有一个内置的依赖注入器,如果您查看他们网站开发人员区域中的指南(http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/ )

还有一个很棒的库,您可以从 NuGet/Github ( https://github.com/aritchie/acr-xamarin-forms ) 中提取它来处理您正在寻找的存储需求...看看设置服务在那里,它甚至可以处理更复杂对象的序列化。

于 2014-06-18T12:12:47.917 回答