Integrating Cortana with the Universal Windows Platform


Application Security Testing: An Integral Part of DevOps


Three mobile platforms, iOS, Android, and Windows, has been improving their digital assistants ("Siri" on iOS platform, "Google Now" on Android, and "Cortana" on Windows) very frequently. All these digital assistants offer search device data and Cloud-based information. Cortana's voice command features integrated into Windows 10 enable developers to give users convenient ways to access data. Cortana offers integration into Bing, Office 365, and local data sources. With a wide range of features, Cortana helps users organize and manage their data easily. Cortana also exposes an interface to developers that permits applications to integrate into the Cortana search experience. In this article, I'll walk through you Cortana integration with the Universal Windows Platform (UWP) travel app.


Step 1

Create a new Windows Universal Project Opening Visual Studio 2015/2017 -> New Project -> Visual C# ->Windows -> Universal -> Blank App.

New Windows Universal App Project
Figure 1: New Windows Universal App Project

New Windows Universal App Project created
Figure 2: New Windows Universal App Project created

Step 2

Next, we have to add a Windows Runtime Component as a new project to the existing solution. This project will act as a background AppService for UWP app and activates whenever the user asks anything of Cortana.

To add, right-click the existing solution -> Add new project -> Add Windows Runtime Component.

New Windows Runtime Component Project
Figure 3: New Windows Runtime Component Project

New Windows Runtime Component Project created
Figure 4: New Windows Runtime Component Project created

Step 3

Next, we have to store the voice commands. Cortana listens to an XML file (Voice Command Definition/VCD file).

Add a Voice Command Definition (VCD) file in your UWP app that needs to be installed on app launch. Do this from CortanaUniversalApp Project -> Add ->New Item ->XML File.

Add new XML VCD file
Figure 5: Add new XML VCD file

Copy and paste the following XML code into your VCD file. A voice command is a single utterance with a specific intent. A VCD file defines one or more voice commands, each with a unique intent.

<?xml version="1.0" encoding="utf-8"?>
      xmlns="http://schemas.microsoft.com/voicecommands/1.2 ">
   <CommandSet xml:lang="en-us" Name="CortanaVSDFile">
         <AppName>Cortena Voice Assistant</AppName>
            Test Command USA Travel
         <Example> Show trip to California </Example>
      <Command Name="showTripToDestination">
         <Example> show trip to California </Example>
         <ListenFor> show trip to {destination} </ListenFor>
            Showing trip to {destination}

      <PhraseList Label="destination">
         <Item> California </Item>
         <Item> Dallas </Item>
         <Item> New York </Item>

      <PhraseTopic Label="newDestination" Scenario="Search">
   <CommandSet xml:lang="en-in" Name="examplevcd">
      <AppName>Voice Assistant</AppName>
            Test Command USA Travel
         <Example> Show trip to California </Example>
      <Command Name="showTripToDestination">
         <Example> show trip to California </Example>
         <ListenFor> show trip to {destination} </ListenFor>
         <Feedback> Showing trip to {destination} </Feedback>

      <PhraseList Label="destination">
         <Item> California </Item>
         <Item> Dallas </Item>
         <Item> New York </Item>

      <PhraseTopic Label="newDestination" Scenario="Search">

Following are the attributes of Cortana's VCD file:

  • AppName: Cortana will listen to the user only with this app name as a prefix of the sentence.
  • Example: Windows shows examples for each individual command.
  • ListenFor: These are the words Cortana listens for.
  • Feedback: Text shows to the user, while working on creating a user response.
  • Navigate: This is the XAML page that Cortana navigates to when it parses what you've said.
  • PhraseTopic: You can add multiple phrases inside this PhraseTopic for better response and understanding of the user.

Step 4

To install the preceding written VSD, add the following async function and call that from the OnLaunched() event of app.xaml.cs file. It will activate the app and call InstallCommandDefinitionsFromStorageFileAsync to register the commands that the system should listen for.

public async Task InstallVCD()
      StorageFile CortanaVSDFileStorage = await
      await Windows.ApplicationModel.VoiceCommands
   catch (Exception ex)
      throw new Exception(ex.ToString());

Step 5

Next, you have to add the reference of the runtime component into the CortanaUniversalApp project.

Choose CortanaUniversalApp Project -> References ->. Right-click ->Add Reference -> Project -> Solution -> Check CortanaUWPDemoBgTask ->. Click OK.

Step 6

Adding Cortana voice commands to a Universal Windows Platform (UWP) app requires a few lines of code to be added to the OnLaunch event, overriding the OnActivated method in the App.xaml.cs file, and overriding the OnNavigatedTo method in any other XAML views that will be opened by the commands.

protected override void OnLaunched(LaunchActivatedEventArgs e)

   #if DEBUG
      if (System.Diagnostics.Debugger.IsAttached)
         this.DebugSettings.EnableFrameRateCounter = true;

   Frame rootFrame = Window.Current.Content as Frame;

   // Do not repeat app initialization when the Window already
   // has content, just ensure that the window is active
   if (rootFrame == null)
      // Create a Frame to act as the navigation context and
      // navigate to the first page
      rootFrame = new Frame();

      rootFrame.NavigationFailed += OnNavigationFailed;

      if (e.PreviousExecutionState ==
         // TODO: Load state from previously suspended application

      // Place the frame in the current Window
      Window.Current.Content = rootFrame;

   if (rootFrame.Content == null)
      // When the navigation stack isn't restored navigate to the
      // first page, configuring the new page by passing required
      // information as a navigation parameter.
      rootFrame.Navigate(typeof(MainPage), e.Arguments);
   // Ensure the current window is active

Step 7

Next, we need to add BackgroundTask into the Windows Runtime component project. This background task will be called on every voice command executed by the user.

Step 8

Lastly, add the following code to the package.appmanifest file and execute the app.

   <uap:Extension Category="windows.appService"
      <uap:AppService Name="CortanaBackgroundTask" />
   <uap:Extension Category="windows.personalAssistantLaunch" />


I believe you learned how to implement Cortana's basic voice commands using VCD files. Cortana's experience can be customized to limit the need for users to continually open and close applications. Also, Cortana allows you to drive a richer engagement with your apps. That's all for today! Keep reading.

About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles


  • Do not follow this instruction!!

    Posted by Harrypotter06 on 10/07/2017 11:28am

    Better, do not follow this instruction. So many information missing, to completed this in a Demo Project. "Task" didnt work, What usings and Nuget Package are required? Not good work from the author

  • xx

    Posted by Bob Kaye on 08/24/2017 07:43am

    How about posting the source code for this tutorial....subject is very topical, but too many omissions in article to follow...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.