Create a Plugin for Xamarin.Forms

Xamarin.Forms enables you to share not only your app’s business logic, but also a shared user interface across iOS, Android, and Windows. Using Xamarin.Form’s built-in dependency service, it enables you to get access to the native platform to expose rich functionality unique to the underlying platform. Using the portable class library (PCL), and NuGet, sharing this code is straightforward. In this article I will create a plugin for logging (logger) in Xamarin Forms.

The plugin will start with a simple interface inside of a PCL describing the functionality to expose and then the implementation for each platform. 

Download the "Plugin For Xamarin Templates" from the link https://visualstudiogallery.msdn.microsoft.com/afead421-3fbf-489a-a4e8-4a244ecdbb1e. This extension (VSIX) contains a template for creating a Plugin for Xamarin.Forms including Xamarin.iOS, Xamarin.Android, Windows Phone, Windows Store, and UWP.

Save the Visual Studio extention (VSIX).

Run the Visual Studio extention by double clicking on the VSIX file. Press the install button.

After the extension is install press the close button. If you are running Visual Studio, make sure you close it and then restart it.

Create a new project with the template.

Select the Plugin for Xamarin template.

This will create the solution from the template. Select "Reload All" to continue.

Remove the Windows Phone 8 project. This version has been deprecated.

Click OK.

Rename the Windows Phone 8.1 project to WinPhone to be consistent with other Xamarin templates.

Rename the Windows 8.1 project to Windows to be consistent with other Xamarin templates.

Use File explorer and delete the Plugin.Logger.WindowsPhone8 directory. This version has been deprecated.

Rename the Plugin.Logger.WindowsPhone81 directory to WinPhone to be consistent with the other Xamarin templates.

Modify the solution file Logger.sln to reflect your changes.

Right click on the solution and select "Manage NuGet Packages for Solution".

Update to the latest Microsoft.NETCore.UniversalWindowsPlatform NuGet package.

Press "OK"

Press "I Accept"

Create a new Xamarin.Forms application. Click on New, Project...

Select the Visual C#, Cross-Platform, Blank Xaml App (Xamarin.Forms Portable)

Select the "OK" Button

Right Click on the solution and select Manage "NuGet Packages for Solution".

Select the Updates Tab. Select the Xamarin.Forms NuGet and Microsoft.NETCore.UniversalWindowsPlatform packages.

Select the "OK" Button.

Select the "I Accept" button.

Select the "Restart" button.

Right Click on the "Plugin.Logger.Abstractions" project and select "New Item..."

Enter "Logger.cs" into the Name: edit box and select the "Add" button.

This is the source code for the LoggerBase Class

Android logger Inplementation

iOS logger Implementation

Windows UWP logger Implementation

Windows Store 8.1 logger Implementation

Windows Phone Implementation

The source code is located on my GitHub site (wpbest/Plugin.Logger): https://github.com/wpbest/Plugin.Logger

No Comments

Add a Comment

As it will appear on the website

Not displayed

Your website