There are many native platform features that exist across all platforms but have slightly different APIs. Developers write plugins to create an abstract cross-platform interface for those features that they can also share with others.
These features include: battery status, compass, motion sensors, geolocation, text-to-speech, and a lot more. Plugins allow these features to be easily accessed by Xamarin.Forms applications.
Finding and Adding Plugins
The Xamarin community has created many cross-platform plugins compatible with Xamarin.Forms - a large collection can be found at:
For a guide to adding NuGet packages to your project, see our walkthrough on including a NuGet package in your project.
It's also possible to create and publish your own plugins as Nuget packages (and Xamarin Components). Many existing plugins are open-source so you can review their code to understand how they have been writtern.
For example, the list of plugins below are all open source, and they correspond
to the samples in the
- Text-to-Speech by James Montemagno – GitHub and NuGet
- Battery Status by James Montemagno – GitHub and NuGet
Those Github projects can provide a good starting point for creating your own cross-platform plugins, as do these instructions for creating a plugin for Xamarin.
Structuring Cross-Platform Plugin Projects
Although there are no particular requirements for designing a NuGet package, there are some guidelines for creating a package for cross-platform apps.
A cross-platform plugin should generally consist of the following components:
- PCL with an Interface that represents the API for the plugin,
- iOS, Android, and Windows class libraries with an implementation of the Interface.
Read James Montemagno's blog post describing the process of creating plugins for Xamarin.
It is a preferable to avoid referencing Xamarin.Forms directly from a plug-in. This can create version-conflict issues when other developers attempt to use the plug-in. Instead try to design the API so that it can be used by any Xamarin or .NET application.
Publishing NuGet Packages
NuGet packages have a nuspec file, which is an xml file that defines which parts of your project are published in the package. The nuspec file also includes information about the package, such as id, title, and authors.
See NuGet's documentation for more information about creating and publishing NuGet packages.