In the two previous blog posts, you should be finished of the first two steps of integrating Cortana.
So, in this blog post, you will perform the last step of integrating Cortana and the most important one which is How to Handle the voice commands.
What we did in the two past blog posts is:
- We Created the VCD file which contains the Voice Commands Which the user should say to Cortana.
- Then, we registered this VCD file on App Startup to let the System/Cortana recognize which command is activated by the user later.
And Now, we will walk through How to Handle the Voice Command when the user Activate it by saying it to Cortana, in other words, What the action should be performed when the user say a specific Command.
Handle Voice Command Activation
Here’s the procedure to handle your voice commands:
- Open App.xaml.cs and then Override OnActivated Method and add this snippet of code:
In this step, we just tells the system when the App is Activated by Voice perform the following, which is based upon the voice command name, then navigate to a Target Page with a parameter(the result of voice command)
- Now, is the real step to Handle the specific Voice Command. To do this add this Code in the TargetPage.xaml.cs which is Mainpage.xaml.cs in our case.
Here, we recieved the parameter from the App.xaml.cs as a SpeechResult and a specific voice command is activated, do the following.
In this example, we are just getting a dictation of what the user says after a voice command is activated(ShowText) using SpeechRecognitionResult.SemanticInterpretation.Properties[“label of the voice command or phrase topic defined in VCD file”][index] Method.
You can Handle your commands as you want using this syntax.
Here’s a full Power Point Presentation of Integrating Cortana.
And Here’s the full Code Sample.
To run the sample, Call Cortana and say “MSP text hello there or whatever”
“MSP text” to activate to voice command.