Skip to main content

Create a Service

important

This tutorial assumes you are using windows and can run the Universal Windows Version of VertiGIS Studio Mobile.

Sometimes, you may have logic or data that needs to be shared by multiple components across an app. In VertiGIS Studio Mobile, services are shared singletons that can register global commands and operations, be injected into components, run background tasks, and more.

Prerequisites#

Check out and setup the VertiGIS Studio Mobile SDK Quickstart project.

Basic Service#

Create a new file services/CustomService.cs under the platform agnostic project. In the file, add a new service class CustomService and register it with VertiGIS Studio Mobile using an assembly attribute.

"App1/App1/services/CustomService.cs
using App1.Services;using VertiGIS.Mobile.Composition;using VertiGIS.Mobile.Composition.Services;using System.Threading.Tasks;
[assembly: Service(typeof(CustomService))]namespace App1.Services{    class CustomService : ServiceBase    {        public CustomService()            :base()        {        }    }}

Create a Custom Command#

Custom Services can register custom commands and operations. The following example shows how a custom service can register a command with the Operations Registry, and how that command can be configured to run on a button press using layout and app config.

"App1/App1/services/CustomService.cs
using App1.Services;using VertiGIS.Mobile.Composition;using VertiGIS.Mobile.Composition.Messaging;using VertiGIS.Mobile.Composition.Services;using System.Threading.Tasks;
[assembly: Service(typeof(CustomService))]namespace App1.Services{    public class CustomService : ServiceBase    {        public CustomService(IOperationRegistry operationRegistry)            : base()        {            operationRegistry.VoidOperation<string>("custom.alert").RegisterExecute((string customMessage) =>            {                return Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Custom Alert", customMessage, "Cancel");            }, this);
        }    }}

Relevant SDK Samples#

Check out the relevant VertiGIS Studio Mobile SDK Samples:

Next Steps#

Learn More About Services

Take a deep dive into services in the VertiGIS Studio Mobile SDK

Build a Service that Manages Dynamic Data

Built a service that manages a dynamic data source and exposes it to components