WP8.1下 Cortana语音命令 VCD文件 设计

Windows Phone8.1下的Cortana,可以通过语音的方式,打开、设置应用,进行页面跳转、执行任务。

我们先要创建VCD(VoiceCommand.xml)文件

<?xml version="1.0" encoding="utf-8" ?> 

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">
 <!--1.0下,不含PhraseTopic元素, 不能识别任意的短语;  1.1下没有此限制-->
<CommandSet xml:lang="zh-cn" Name="CommanSet1">  <!--中文--> <CommandPrefix>快递</CommandPrefix>  <!--程序名, 无次元素则为默认程序名(这个只是为了说话方便设定的)--> <Example>查快递</Example>   <!-- 以上两个将会显示在Cortana查看更多内-->

  <!--当上面点入后会进入到说话示例--> <!--命令-->

    <Command Name="Command1">
      <Example>扫描条形码</Example>
      <ListenFor>扫描条形码</ListenFor>
      <Feedback>跳转到条形码扫描页面</Feedback>
      <Navigate/>
    </Command>

<Command Name="Command2"> <Example>查询 + {快递名称}</Example> <ListenFor>查询{ExpressName}</ListenFor>   <Feedback>查询{ExpressName}</Feedback>   <Navigate/> </Command> <Command Name="Command3"> <Example>查询 + {快递名称} + {运单号}</Example> <ListenFor>[查询] {ExpressName} [运单][运单号] {order}</ListenFor> <!-- []表示可有可无的单词或短语 --> <Feedback>查询 {ExpressName} 运单号:{order}</Feedback> <Navigate/> </Command> <!--0个或一个--> <PhraseList Label="ExpressName"> <!--用大括号 {}引用 同时用于ListenFor、Feedback元素中--> <Item>申通快递</Item> <Item>圆通快递</Item> <Item>顺丰快递</Item> </PhraseList> <PhraseTopic Label="order" Scenario="Short Message">  <!--可识别任意语句--> <Subject>Order</Subject> </PhraseTopic> </CommandSet> </VoiceCommands>

上面的VCD文件中3个语音命令功能:

Command1: 识别“快递 扫描条形码”,并跳转到扫描页面。

Command2: 识别“快递 查询{快递名称}”, 并跳转到主页面,将识别到的快递名称;      {快递名称}为申通、圆通、顺丰中一个

Command3: 识别“快递 [查询] {快递名称} [运单][运单号] {自己说的话,这里为运单号}”;      []为可有可无

这三个命令应该可以代表所有了吧。 

 

 VCD文件创建好后,就开始初始化VCD文件 主页面中:

using Windows.Media.SpeechRecognition;

protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (VoiceCommandManager.InstalledCommandSets.Count == 0)
            {
                StorageFile file = await Windows.ApplicationModel.Package.Current.
                    InstalledLocation.GetFileAsync("VoiceCommand.xml");
                await VoiceCommandManager.InstallCommandSetsFromStorageFileAsync(file);
            }
    }

从Cortana执行语音命令

App.cs中

protected override void OnActivated(IActivatedEventArgs args)
        {
            if (args.Kind == ActivationKind.VoiceCommand)  //应用在语音命令后激活
            {
                VoiceCommandActivatedEventArgs voiceCommandArgs = args as VoiceCommandActivatedEventArgs;
                Frame rootFrame = new Frame();

           string result = voiceCommandArgs.Result.Text;  //识别到的结果          
         if (result.Contains("运"))
                 rootFrame.Navigate(typeof(MainPage), voiceCommandArgs);//传递参数,之后在新页面接受处理
  else if (result.Contains("条形码"))   rootFrame.Navigate(typeof(BarCodeScanPage)); else if (result.Contains("查询"))
                    rootFrame.Navigate(typeof(MainPage), voiceCommandArgs);
Window.Current.Content = rootFrame; Window.Current.Activate(); } }

这样就完成了,小娜真的很好用. 以后的应用应该很多都会支持语音命令吧。

posted @ 2014-11-26 10:01  汪小饭  阅读(551)  评论(0编辑  收藏  举报