Write device platform specific code in Xamarin.Forms

前端 未结 5 1385
野性不改
野性不改 2020-12-07 18:57

I have the following Xamarin.Forms.ContentPage class structure

public class MyPage : ContentPage
{
    public MyPage()
    {
        //do work t         


        
相关标签:
5条回答
  • 2020-12-07 19:34

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

    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);
    }
    

    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)
        {
            (...)
        }
    }
    

    Android:

    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)
        {
            (...)
        }
    }
    

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 19:35

    Xamarin.Forms has a built-in dependency injector if you take a look at their guide in the developer area of their website (http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/)

    There's also a wonderful library you can pull from NuGet/Github (https://github.com/aritchie/acr-xamarin-forms) that will handle the storage requirement you are looking for... take a look at the Settings service in there, it will even handle serialization of more complex objects.

    0 讨论(0)
  • 2020-12-07 19:42

    Xamarin.Forms 2.3.4 introduced a new method for this:

    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
    }
    

    There are also other platforms to choose from, you can type in Device. in Visual Studio and it will show you the options.

    0 讨论(0)
  • 2020-12-07 19:46

    There are multiple answers, depending on what you want to achieve, and the kind of project you have:

    Execute different Xamarin.Forms code on different platforms.
    Use this e.g. if you want different font sizes on different platforms:

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

    Execute platform specific code in a shared (PCL) project The common pattern is to use DI (dependency injection) for this. Xamarin.Forms provides a simple DependencyService for this, but use whatever you want.

    Execute platform specific code in shared (Shared Asset Project) project As the code is compiled per platform, you can wrap your platform specific code in #if __PLATFORM__ #endif and have all the code in the same file. The platform project should define __IOS__, __ANDROID__ and __WINDOWS_PHONE__. Note that a shared asset project containing Xaml and code won't work well for iOS on Xamarin.Studio, and that having compiler directives makes your code harder to read and to test.

    0 讨论(0)
  • 2020-12-07 19:47

    This seems less about Xamarin.Forms and more about using defaults in a PCL. Check out James Montemagno's github repo for doing cross-platform defaults.

    Then just call his static methods for setting/retrieving. The nuget package is Xam.Plugins.Settings.

    It can be used like this:

    using Refractored.Xam.Settings;
    

    ...

    CrossSettings.Current.AddOrUpdateValue("AccessToken", accessToken);
    var value = CrossSettings.Current.GetValueOrDefault<string>("AccessToken");
    
    0 讨论(0)
提交回复
热议问题