微软小娜开发教程<二>
2015-10-14 16:47
495 查看
利用语音指令通过Cortana开启前台应用
利用语音指令除了能打开系统应用之外,还能打开前台应用并在应用内指定执行的操作或命令。1、定义语音指令文件
新建一个空的UWP应用,往工程中添加一个空的XML文件。语音指令特指在VCD(voice command definition)文件中按照一定格式定义的词语组合。
举个栗子,这个VCD文件是一个名叫“Adventure Works”的工程下的一个XML文件:
<?xml version="1.0" encoding="utf-8"?> <VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1"> <CommandSet xml:lang="en-us" Name="AdventureWorksCommandSet_en-us"> <CommandPrefix> Adventure Works, </CommandPrefix> <Example> 显示伦敦之行 </Example> <Command Name="showTripToDestination"> <Example> 显示伦敦之行 </Example> <ListenFor> 显示去 {destination} 的旅行</ListenFor> <Feedback> 正在显示去 {destination} 的旅行</Feedback> <Navigate/> </Command> <PhraseList Label="destination"> <Item> 伦敦</Item> <Item> 纽约</Item> <Item> 北京</Item> </PhraseList> <PhraseTopic Label="newDestination" Scenario="Search"> <Subject>City/State</Subject> </PhraseTopic> </CommandSet> <!-- Other CommandSets for other languages --> </VoiceCommands>
这个XML文件里就定义了一个语音命令”Adventure Works,显示去伦敦的旅行“。这里
的语音指令很灵活,可以在PhraseList 标签中动态添加。
VCD文件动态添加指令指南
2、安装VCD命令
当应用激活时,需要在OnLaunched处理程序中调用InstallCommandDefinitionsFromStorageFileAsync以注册系统应该侦听的命令。在App.xaml.cs文件下的Onlaunched函数中添加如下代码段
....... Window.Current.Activate(); try { var storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync( new Uri("ms-appx:///VoiceCommands.xml")); await Windows.ApplicationModel.VoiceCommands.VoiceCommandDefinitionManager. InstallCommandDefinitionsFromStorageFileAsync(storageFile); } catch (Exception) { throw; }
处理激活并执行语音命令
安装语音指令集后,指定应用如何响应后续指定操作或指令动作。例如,你的应用可能导航到特定页的内容、显示地图或说出确认语句等。执行语音指令的操作,是最终最终愿景。需要在重写OnActivated()函数里干三件事:
1. 确认你的应用已经通过语音命令激活。
2. 确认命令名称和说出的内容。
3. 执行动作,通常是导航到相关页面。
这里写代码片 protected override void OnActivated(IActivatedEventArgs e) { // Was the app activated by a voice command? if (e.Kind != Windows.ApplicationModel.Activation.ActivationKind.VoiceCommand) { return; } var commandArgs = e as Windows.ApplicationModel.Activation.VoiceCommandActivatedEventArgs; Windows.ApplicationModel.VoiceCommands.VoiceCommand.SpeechRecognitionResult speechRecognitionResult = commandArgs.Result; // Get the name of the voice command and the text spoken string voiceCommandName = speechRecognitionResult.RulePath[0]; string textSpoken = speechRecognitionResult.Text; // The commandMode is either "voice" or "text", and it indicates how the voice command was entered by the user. // Apps should respect "text" mode by providing feedback in a silent form. string commandMode = this.SemanticInterpretation("commandMode", speechRecognitionResult); switch (voiceCommandName) { case "showTripToDestination": // Access the value of the {destination} phrase in the voice command string destination = speechRecognitionResult.SemanticInterpretation.Properties["destination"][0]; // Create a navigation parameter string to pass to the page navigationParameterString = string.Format("{0}|{1}|{2}|{3}", voiceCommandName, commandMode, textSpoken, destination); // Set the page where to navigate for this voice command navigateToPageType = typeof(TripPage); break; default: // There is no match for the voice command name. Navigate to MainPage navigateToPageType = typeof(MainPage); break; } if (this.rootFrame == null) { // App needs to create a new Frame, not shown } if (!this.rootFrame.Navigate(navigateToPageType, navigationParameterString)) { throw new Exception("Failed to create voice command page"); } }
到此为止,一个简单的通过小娜来开启前台的应用就完成了。
下面提供两个实用例程,源文件可下载。
1、通过小娜来开启一个简单的前台应用
2、Cortana语音命令实例–MSDN_demo
相关文章推荐
- Ubuntu14.04环境下各种格式的安装包的使用方法
- 合并百度地图API真机和模拟器的.a文件
- Difference between val() and text()
- lua中字符串匹配笔记
- 爬爬爬之路:C语言(五) 二维数组篇
- linux下怎么查看ssh的用户登录日志
- 全面理解Unity加载和内存管理
- struts2 基本用法
- Oracle DISTINCT A 排序问题(转)
- 最小化安装centos的init初始化脚本
- node.js was not found in the default path
- RHadoop的安装与试验
- mysql体系结构
- SQL SERVER 2012启动失败 because upgrade step 'SSIS_hotfix_install.sql' 失败
- sql server使用中遇到的问题记录
- [API]common http get and post
- Log4j.properties 详解
- ajax 实现当输入用户名或手机号找回密码的前台操作
- python中获得某月有多少天的函数
- ExtJs 实现动态列,动态多表头