Create a Service

important

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

Sometimes, you may have logic or data that needs to be shared by multiple components across an app. In Geocortex 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 Geocortex 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 Geocortex Mobile using an assembly attribute.

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.

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 Geocortex Mobile SDK Samples:

Next Steps#

Learn More About Services

Take a deep dive into services in the Geocortex Mobile SDK

Build a Service that Manages Dynamic Data

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