Skip to content

Cortana integration tutorial

Jakub Lichman edited this page Apr 20, 2017 · 1 revision

In this tutorial we are going to show you how to integrate your UWP app with Cortana. If you have some problems with Cortana on your device, please check FAQ page for possible solutions.

Step1 - Create VCD file

  1. Creation of VCD file is perfectly described in this article.
  2. If you don't have enough examples, please refer to our commands that are used in PiStudio.

Step2 - Install VCD file into Cortana

App should during first run or in case of some changes in VCD file install commands into Cortana. You can install them in two ways:

  • First one is shorter, but commands will be installed every single time
var cortanaFile = await StorageFile.GetFileFromPathAsync(@"Path\to\your\commands.xml");
var integrator = await CortanaIntegrator.Create(cortanaFile);
  • Second one enables you to decide whether install commands or not
var pathToCommandsFile = @"Path\to\your\commands.xml";
var integrator = new CortanaIntegrator();

//if commands were not installed
var result = await integrator.Install(pathToCommandsFile);

Step3

After successful installation we need to attach actions to commands. We will do it by calling SetAction method. First argument specifies command set, where is command located, second one our targeted command and third one callback to the action that will be performed after successful recognition.
Here is full example including previous step:

protected override async void OnActivated(IActivatedEventArgs args)
{
    base.OnActivated(args);
    var cortanaFile = await StorageFile.GetFileFromPathAsync("Path\to\your\commands.xml");
    var integrator = await CortanaIntegrator.Create(cortanaFile);

    integrator.SetAction("MyCommandSet", "MyCommand", callback);
    if(args.Kind == ActivationKind.VoiceCommand)
    {
         integrator.PerformAction((VoiceCommandActivatedEventArgs)args);
    }
}

private async void callback(object sender, SpeechRecognitionResult e)
{
    //your code here
}

//do not forget to install commands during normal launch
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
    var cortanaFile = await StorageFile.GetFileFromPathAsync("Path\to\your\commands.xml");
    await CortanaIntegrator.Create(cortanaFile);
}
Clone this wiki locally