Adding Cortana to your Windows 10 App
The Cortana voice command features integrated into Windows 10 enable developers to give users convenient ways to access data that usually requires several user actions (tapping, swiping, typing, etc.) or time to find (looking through a long list of items or reading a large piece of text). Three examples where this functionality would be helpful are:
- Case 1 - A hospital: The staff uses Cortana to find the closest open bed for a newly admitted patient
- Case 2 - A retail store: A sales associate finds whether a product is in stock for a customer
- Case 3 - A shopper: A shopper tries to find the location of their favorite store in a large shopping mall
Making it more convenient for users to access relevant data will enable apps to drive more value, since users will be able to access the information they seek more quickly.
Adding Cortana voice commands to a Universal Windows Platform (UWP) app requires adding two additional files to a project, adding a few lines of code 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.
- VoiceCommands.xml
- defines the voice commands that Cortana should recognize associated with the app (XML schema)
- VoiceCmdService.cs
- specifies the behavior Cortana exhibits when the command is found, such as icons displayed and text shown on successful results
- Add to the
OnLaunch
event- Before the
Window.Current.Activate();
method execution in theOnLaunch
event, please insert the following
- Before the
var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///VoiceCommands.xml"));
await Windows.ApplicationModel.VoiceCommands.VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(storageFile);
// OPTIONAL!!! Used to dynamically populate the phrase list for a command
Windows.ApplicationModel.VoiceCommands.VoiceCommandDefinition vcDef;
if (Windows.ApplicationModel.VoiceCommands.VoiceCommandDefinitionManager.InstalledCommandDefinitions.TryGetValue("CommandSet_en-us", out vcDef))
await vcDef.SetPhraseListAsync("section", new string[]{"schedule","mail","tasks"});
- Methods to override
OnActivated
- Located in the App.xaml.cs file. Specify which XAML page will be navigated to based on the voice command invoked.
if (args.Kind == ActivationKind.VoiceCommand)
{
VoiceCommandActivatedEventArgs voiceArgs = args as VoiceCommandActivatedEventArgs;
SpeechRecognitionResult speechRecognitionResult = voiceArgs.Result;
// Access the value of the {section} phrase in the voice command
section = this.SemanticInterpretation("section", speechRecognitionResult);
}
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
// Specify what page should be loaded due to the command
// NOTE: This can be an if or case statement depending on the number of commands
rootFrame.Navigate(typeof(MainPage), section);
Window.Current.Activate();
}
///
/// Returns the semantic interpretation of a speech result. Returns null if there is no interpretation for
/// that key.
///
/// The interpretation key.
/// The result to get an interpretation from.
///
private string SemanticInterpretation(string interpretationKey, SpeechRecognitionResult speechRecognitionResult)
{
return speechRecognitionResult.SemanticInterpretation.Properties[interpretationKey].FirstOrDefault();
}
<dl>
<dt style="width:150px;"><code class="cs">OnNavigatedTo</code></dt>
<dd style="margin-left:170px;font-size:14px;">Located in XAML views. Specify how content on the page will be displayed based on the parameters specified in the command.</dd>
</dl>
```cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.Parameter is string && (e.Parameter as string) != string.Empty)
{
// Get the parameter value and cast it to the class type it was passed as
string section = (e.Parameter as string);
// Loading and manipulating content based on the parameter specified
}
}
Augmenting applications with voice commands would work as verbal shortcuts to improve content consumption by users. By creating data driven apps that enable users to ask specific and useful questions, an app can be more transparent to a user and improve its adoption and usage.