Microsoft has been launching Cortana for some time now, just studying its usage, and writing a few notes about it.
In the study, I refer to the blog of @ Wang Bo _nick : http://www.cnblogs.com/sonic1abc/p/3868729.html, thank you first.
Gossip doesn't say much, let's get started.
To add Voice command functionality to your app, you need three steps:
1. Create a Voice command definition (VCD) file. This is an XML document that defines all the voice commands that the user can say to initiate an action when the app is activated.
First add a new VCD file to the project,
The new files are created as follows:
The following is a brief description of each label:
Voicecommands is the beginning of the VCD file, indispensable. The newly created VCD file defaults to the Windows Phone 8.0 template, and if you want to apply Cortana, change the 1.0 after voicecommands to 1.1.
<xmlns= "http://schemas.microsoft.com/voicecommands/1.1">
Change to 1.1 will not define the Phraselist label, instead of the phrasetopic. By contrast, Phrasetopic is more flexible, it can represent anything the user says, and phraselist can only represent what is listed in item.
CommandSet represents the command set and defines the language of the command. To support the Chinese command, you need to modify the Xml:lang property to ZH-CN.
<xml:lang= "ZH-CN">
Commandprefix represents the command prefix, which is to shout the word to Cortana before calling out the command, and you can call the app's name in addition to the prefix. The Commandprefix is unique in the file. If the app is called Application,commandprefix, you can shout to Cortana: The application (APP) XXX command.
Example is a message to the user that tells the user which voice commands the app accepts. The hints in Cortana will display the combination of commandset and command example.
Consider the example:
the command represents the individual voice commands, the format of the commands specified in thelistenfor , such as: [to] play level {levels} [game]. The contents in brackets are optional commands that can be shouted without shouting, Cortana recognizes automatically, and the contents in curly braces represent what the app needs to be processed, and the variable name inside it needs to be defined as phrasetopic.
Feedback represents the voice content that Cortana will play before opening the app, and if you don't want to hear it you can use symbols instead ... Navigate is the name of the page that navigates to the app, that is, Cortana will open the corresponding page of the app directly after you receive the command.
<Command Name = "Search">
<Example> Search xx </ Example>
<ListenFor> Search [question] {keyword} </ ListenFor>
<ListenFor> Find {keyword} </ ListenFor>
<ListenFor> Check {keyword} </ ListenFor>
<Feedback> ... </ Feedback>
<Navigate Target = "SearchPage.xaml" />
</ Command>
<PhraseTopic Label = "keyword" Scenario = "Search">
</ PhraseTopic>
2. Add the code to your app to register the command set in a VCD file with phone speech capabilities.
You can register a VCD file with the system when the application starts, and the method of registering is simple. If your application wants to support both 8.0 and 8.1, you will need to determine the system version to load different VCD files.
1 private const string Wp81VcdPath = "ms-appx: ///VoiceCommandDefinition81.xml";
2 private const string Wp80VcdPath = "ms-appx: ///VoiceCommandDefinition80.xml";
3
4 public async void RegisterVcd ()
5 {
6 var using81OrAbove = ((Environment.OSVersion.Version.Major> = 8)
7 && (Environment.OSVersion.Version.Minor> = 10));
8 var vcdPath = using81OrAbove? Wp81VcdPath: Wp80VcdPath;
9
10 try
11 {
12 // Register VCD file with the system
13 await VoiceCommandService.InstallCommandSetsFromFileAsync (new Uri (vcdPath));
14}
15 catch (Exception ex)
16 {
17 Debug.WriteLine (ex.HResult + ex.Message);
18}
19}
3. Add code to the app to process voice commands to activate, navigate, and execute the command.
When the app starts with Cortana, the page's Navigationcontext property contains a parameter called "Voicecommandname" that corresponds to the Name of the Command label defined in the VCD file property. Then different processing is made according to different commands.
My own approach is to encapsulate a speech processing helper class and then pass out different events.
/// <summary>
/// The processing method of the voice command is called after listening to the voice command event. If the program is not started through the voice command, the voice command file (VCD file) will be registered with the system
/// </ summary>
/// <param name = "context"> NavigationContext of the page where this method is called </ param>
public static void CommandProcess (NavigationContext context)
{
_commandContext = context;
string commandName;
if (_commandContext.QueryString.TryGetValue ("voiceCommandName", out commandName))
{
HandleCommand (commandName);
}
}
Private static void HandleCommand (string commandName)
{
string content;
if (_commandContext.QueryString.TryGetValue ("keyword", out content)
&&! string.IsNullOrEmpty (content))
{
switch (commandName)
{
case "PageSwitch":
OnPageConvertCommandExecuted (content);
break;
case "WordCopy":
OnLanguageCopyCommandExecuted (content);
break;
case "Search":
OnSearchCommandExecuted (content);
break;
case "OpenFunction":
OnOpenThingsCommandExecuted (content);
break;
}
}
}
Then subscribe to the event in the corresponding page
protected override void OnNavigatedTo(NavigationEventArgs e)
{ base.OnNavigatedTo(e); if (e.NavigationMode == NavigationMode.New)
{
VoiceCommandHelper.WordCopyCommandExecuted += VoiceCommandHelper_WordCopyCommandEventExecuted;
VoiceCommandHelper.OpenFunctionCommandExecuted += VoiceCommandHelper_OpenFunctionCommandEventExecuted;
VoiceCommandHelper.CommandProcess(NavigationContext);
}
}
Resources:
Quickstart:voice commands (XAML)
QuickStart: Voice Commands (XAML)
Source code download for MSDN:
MSDN Voice Search for Windows Phone 8.1
My own Source: http://pan.baidu.com/s/1ntjyR37
I hope you crossing more bricks
Windows Phone 8.1 app integrates Cortana voice commands