Press "Enter" to skip to content

Using Prism + Shiny in Xamarin Forms

Working with background services in mobile applications can be a little be tricky, but now we have a great option called Shiny which is a library for Xamarin & UWP developed by Allan Ritchie that enables you to work easily with background services & device hardware.

In this article, I will show you how to set up Shiny step by step in a Xamarin Forms project that uses Prism, which is a framework that embraces best coding practices to develop loosely coupled, maintainable and testable applications (If you haven’t used it before I recommend you to read this series of articles about it by XamGirl).

 

Let’s start! 

1. Install the following packages:

NOTE:

For this example, we will be using the Dryloc container, if you want to use the Unity container you should install Prism.Unity.Forms.Extended instead.

 

2. Configure your App.xaml class

1. Replace the Application class for PrismApplication

2.Extend your App class from PrismApplication.

3.Register your pages for navigation and set your initial page navigation

4.Override CreateContainerExtension method and return PrismContainerExtension.Current.

3. Create your Prism Startup class 

In your Xamarin Forms project create a new class that extends from PrismStartup class. Then call base constructor passing PrismContainerExtension.Current as the parameter.

This class will be used to register your shiny services.

4. Configure your iOS project

In your AppDelegate class initialize Shiny by calling iOSShinyHost.Init passing your startup class as the parameter (should be the first call in the FinishedLaunching method).

5. Configure your Android project

In your Android project create a new Application class that extends from generic class ShinyAndroidApplication with your startup class specified as the parameter type.

In your MainActivity override the OnRequestPermissionResult method and add the AndroidShinyHost.OnRequestPermissionsResult call.

Use Shiny  

Shiny has a lot of functionalities such as GPS location, Geofencing, Beacons, Bluetooth, Sensors, Notifications, etc. To use each of then you have to install the NuGet package of that contains that functionality. For example, if we want to use GPS location you have to install the package Shiny. Locations.

Then, you have to register the GPS service in the ShinyAppStartup file.

Also must setup the needed permissions depending on the platform. More info here: https://shinydocs.azurewebsites.net/docs/locations/gettingstarted

To use it:

You can check the full source code here.

Reference