Share Even More Code with Plugins for Xamarin

Pierce Boggan

Plugins for Xamarin NuGet IconOne of my top priorities for Moments, a Snapchat clone built with Xamarin.Forms and Microsoft Azure, was to maximize the amount of code shared between my target platforms. I was already achieving a significant amount of shared code with Xamarin.Forms, but I noticed that many things, such as checking for device connectivity or showing a dialog, had to be done on a per-platform basis. Plugins for Xamarin helped fill in those gaps and allowed me to share even more code.

 

A Plugin for Xamarin is a special kind of NuGet that adds cross-platform functionality or abstracts platform-specific functionality to a common API. These are extremely lightweight and completely cross-platform, so they can be accessed via a Portable Class Library (PCL) or Shared Project. Below, I’ve shared a few of my favorite Plugins for Xamarin that I used to build Moments.

Connectivity

In today’s world, it’s vital that apps maintain connectivity when interacting with web services and other network-dependent logic. With the Connectivity Plugin for Xamarin and Windows, it’s never been easier to check the connection status of a mobile device, gather connection types and bandwidths, and more.

Testing network connectivity from shared code is literally as easy as writing one line of code:

var connected = CrossConnectivity.Current.IsConnected;

The Connectivity Plugin for Xamarin and Windows can also test the reachability of specific hosts using the IsReachable and IsRemoteReachable methods. Finally, you can subscribe to the ConnectivityChanged event to ensure that your app always knows if the device is connected to the internet:

CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
{
// Connectivity changed! Check args.IsConnected!
};

Settings

Another common use case among mobile apps is the ability to store user settings. Each platform exposes its own way of storing settings, such as NSUserDefaults on iOS and SharedPreferences on Android. The Settings Plugin for Xamarin and Windows takes the ability to create and access settings cross-platform!

Rather than dealing with the various platform-specific implementations, the Settings Plugin for Xamarin and Windows makes it easy to implement user settings, as seen below:

public class Settings
{
public bool NotificationsEnabled
{
get { CrossSettings.Current.GetValueOrDefault<bool> ("notificationsEnabled"); }
set { CrossSettings.Current.AddOrUpdateValue<bool> ("notificationsEnabled", value); }
}
}

Not only is the Settings Plugin for Xamarin and Windows great for managing settings from shared code, but it can also act as a simple cross-platform key-value store with support for many different data types, such as strings, integers, doubles, booleans, DateTimes, and more!

Dialogs

From alert popups to loading spinners, dialogs are a staple of mobile app development. The User Dialogs Plugin for Xamarin and Windows allows you to show almost any type of dialog you can think of from shared code, including loading spinners, alerts, confirmations, and error dialogs.

Loading Indicators For performing potentially lengthy tasks, such as logging in a user, use the ShowLoading method:

UserDialogs.Instance.ShowLoading ("Loading");

When the task ends, you can hide the dialog using the following method call:

UserDialogs.Instance.HideLoading ();

Success & Failure If a dialog needs to reflect success or failure, such as the the result of a user log in, you can use the success and failure APIs available within the User Dialogs Plugin for Xamarin and Windows:

// Success
UserDialogs.Instance.ShowSuccess ("Success");
// Failure
UserDialogs.Instance.ShowError ("Error");

Download a Plugin for Xamarin Today

All of the Plugins for Xamarin listed above helped me significantly increase productivity by achieving over 87% shared code in Moments. Check out Plugins for Xamarin on GitHub to find a plugin for your next project, or find out how easy it is to build your own Plugin for Xamarin.

0 comments

Discussion is closed.

Feedback usabilla icon