user2686744
user2686744

Reputation:

Create a Xamarin.Forms library from a Xamarin.Android / Xamarin.IOS library

Maybe this is a very basic question but here it is :

I have 2 libraries :

What's the simplest way to create a Xamarin.Forms library that wraps the native libraries and will call them when needed?

Upvotes: 1

Views: 1239

Answers (2)

pnavk
pnavk

Reputation: 4632

Assuming both the libraries do the same thing: you can use the strategy pattern to expose those libraries to X.Forms:

1.) In your X.Forms project, create a common Interface that exposes the methods you want to call in the libraries:

public interface ICommonService
{
  void DoSomething();
  void DoSomethingElse();
}

2.) In both of your platform projects, provide a concrete implementation of this interface:

public class CommonService : ICommonService
{
  public void DoSomething(){...}
  public void DoSomethingElse(){...}
}

Because the concrete implementation are in your platform projects, they have access to the Xamarin.Android and Xamarin.iOS libraries you want to leverage.

3.) Use dependency injection to create an instance of the service in your X.Forms project. One way is to use the DependencyService in your X.Forms project:

DependencyService.Get<ICommonService>().DoSomething();

Depending on if you are running the iOS or Android app, this will call the provided concrete implementation.

Note: If you plan on using DependencyService, you will need to register your concrete implementations first.

The following documentation has a very nice walkthrough of how to use the Xamarin.Forms DependencyService to achieve the strategy pattern: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

Upvotes: 0

valdetero
valdetero

Reputation: 4652

I assume you mean actual platform libraries and not apps. If you do mean the apps: You're thinking of the projects backwards. The X.Forms library doesn't reference or call the platform libraries (X.Android & X.iOS); the platform libraries call the Xamarin.Form library. Just make sure both platform implementations implements the same interface and code against that interface in the Xamarin.Forms project.

If you're creating something like a plugin or control to use in Xamarin.Forms then you need to jump through a few hoops. I would suggest following James Montemagno's pattern(s). (He's a PM for Xamarin that makes a ton of nugets)

  1. Use the old Visual Studio Extension that will stub out the projects for you. Unfortunately it uses PCLs.
  2. Use the pattern he uses for Xamarin.Essentials. It uses fancy build conditions and only has one project.

If you want to hand roll a reusable control/nuget it yourself, you will need an abstraction to code against in your Xamarin.Forms library and some form of Dependency Injection to get to the native implementation.

Here is a good blog post about PCL bait and switch (which is what happens with the nugets)

tl;dr You will need dependency injection. If aren't already using it, use the DependencyService that comes with Xamarin.Forms.

Upvotes: 2

Related Questions