Sergio Di Fiore
Sergio Di Fiore

Reputation: 466

Calling specific iOS/Android routines from shared

I have this piece of code that runs in iOS:

  namespace Login.iOS
    {
    class Authenticate
    {
        public async Task LoginAsync()
        {
            var client = new Auth0Client(new Auth0ClientOptions

            {
                Domain = "difiore.auth0.com",
                ClientId = "Key goes here"
            });

                var loginResult = await client.LoginAsync();
            }
        }
    }

As well as its equivalent for Android:

    namespace Login.Droid
    {
    class Authenticate
    {
        public async Task LoginAsync()
        {
            var client = new Auth0Client(new Auth0ClientOptions

            {
                Domain = "difiore.auth0.com",
                ClientId = "Key goes here"
            });

            var loginResult = await client.LoginAsync();
        }
    }
}

As both depends upon libraries that are specifically written for the platform, they must be platform specific.

Then I have the shared code that should call then:

    namespace Login
    {
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentPage
    {
        public MainPage ()
        {
            InitializeComponent ();
        }

        private void Login_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new Authenticate());
        }
    }
}

But the code line

Navigation.PushAsync(new Authenticate());

is obviously wrong as it doesn't identify the routine to be called.

How can I call the platform specific routine from the shared one? That is the Authenticate class in IOS and Android.

Upvotes: 1

Views: 72

Answers (1)

Judson Abraham
Judson Abraham

Reputation: 412

You have to create an interface for calling platform specific class in shared or portable class using Dependency Injection. Follow this link https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction. I hope this helps.

Upvotes: 2

Related Questions