Skip to main content

Build a Custom Command

tip

VertiGIS Studio Mobile has many built-in Commands and Operations which can be chained together to build custom behaviors. If these Commands and Operations are not enough to accomplish your goal, you can use VertiGIS Studio Workflow to take custom behavior even further without writing custom code. If Workflow is still not enough to accomplish your needs, then it might be time to implement a Custom Command / Operation.

This article will walk you through implementing a custom command in VertiGIS Studio Mobile that displays an alert.

Prerequisites

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

Create the Backing Service

It's convention that commands and operations in VertiGIS Studio Mobile are registered and implemented by a service or component, so the first thing we will do is create a service.

  1. Create a new file Services/AlertService.cs under the platform agnostic project.
App1/App1/services/AlertService.cs
using App1.Services.AlertService;
using VertiGIS.Mobile.Composition;
using VertiGIS.Mobile.Composition.Services;

[assembly: Service(typeof(AlertService), PropertiesAutowired = true)]
namespace App1.Services.AlertService
{
class AlertService : ServiceBase
{
}
}
  1. Create a class Services/AlertOperations under the platform agnostic project.
    • In the file, add a new class AlertOperations and register it with Autofac.
App1/App1/services/AlertOperations.cs
using App1.Services.AlertOperations;
using VertiGIS.Mobile.Composition;
using VertiGIS.Mobile.Composition.Messaging;
using VertiGIS.Mobile.Infrastructure.Messaging;

[assembly: Export(typeof(AlertOperations), SingleInstance = true)]
namespace App1.Services.AlertOperations
{
class AlertOperations : OperationsBase
{
public AlertOperations(IOperationRegistry operationRegistry)
: base(operationRegistry)
{
}
}
}

Implementing the Operation

Creating a new operation in VertiGIS Studio Mobile consists of two pieces:

  • Defining the operation's name, inputs, and outputs in the OperationsBase class
  • Registering an execution implementation in the service.
    • This function will be the actual code that executes when the operation is called.

Define the Operation

For this example, let's define a operation with some input and no output.

class AlertOperations : OperationsBase
{
public IVoidOperation<string> DisplayCustomAlert => GetVoidOperation<string>("custom-alert.display");

public AlertOperations(IOperationRegistry operationRegistry)
: base(operationRegistry)
{
}
}

Register an Implementation for the Operation

Next, we need to register an implementation for our operation in the AlertService.

  1. Add a public constructor that takes an AlertOperations to the Service.
    [assembly: Service(typeof(Service), PropertiesAutowired = true)]
class AlertService : ServiceBase
{
public AlertService(AlertOperations alertOperations)
{
}
}
  1. Create an implementation for displaying a custom alert and register it as an execution for the operation.
[assembly: Service(typeof(AlertService))]
namespace App1.Services
{
class AlertService : ServiceBase
{
public AlertService(AlertOperations alertOperations)
{
alertOperations.DisplayCustomAlert.RegisterExecute(
(args) =>
{
Application.Current.MainPage.DisplayAlert("Custom Alert", args, "OK");
return Task.FromResult((object)null);
}, this);
}
}
}

Congratulations! You just created your first custom operation. You can easily test your new operation by setting it to run it on map initialization.

{
"$schema": "..\\..\\ViewerFramework\\app-config\\mobile\\mobile-app-config.schema.json",
"schemaVersion": "1.0",
"items": [
...
{
"$type": "map-extension",
"id": "map1",
"onClick": [ "tasks.identify", "highlights.pulse", "results.display" ],
"onInitialized": [
{
"name": "custom-alert.display",
"arguments": "This is a custom operation!"
}
]
},
...
]
}

Relevant SDK Samples

Check out the relevant VertiGIS Studio Mobile SDK Samples:

Next Steps

Commands and operations are the logical building blocks of executing custom behavior in VertiGIS Studio Mobile, but sometimes you need to take things a bit further. Creating custom components can allow you to design completely custom UI for VertiGIS Studio Mobile, and creating background services can enable the execution of tasks on an arbitrary schedule.

Commands and Operations

Learn more about commands and operations

Implement a Custom Component with UI

Learn how to implement a custom component using the VertiGIS Studio Mobile SDK

Implement a Custom Service

Learn how to implement a custom service using the VertiGIS Studio Mobile SDK