Win10/UWP开发—使用Cortana语音与App后台Service交互

上篇文章中介绍了(53078485群)大咖Aran的博客:Win10/UWP开发—使用Cortana语音指令启动前台App ;而这篇博客我们将接着上篇文章的内容为大家带来大咖Aran的讲解:讲讲如何使用Cortana调用App的后台任务。


相比Cortana语音调用前台的App,调用后台任务有个优点就是App不用被启动即可为用户提供服务。

要想使用Cortana调用App后台任务,首先我们需要定义VCD文件,我们依旧使用上篇中的代码,让它支持Cortana调用后台任务。

 
创建后台任务

新增一个[Windows运行时组件]项目,暂时起名叫做:XiaoMiBackgroundTask

创建一个类,暂且叫做XiaoMiTask,并继承IBackgroundTask

完成如下代码:
//------------------------------------------------------
//
// FileName:    XiaoMiTask.cs
// Namespace:   XiaoMiBackgroundTask
// Assembly:    XiaoMiBackgroundTask
// Description:     
// Author:      aran_wang
// Created On:  2015-09-10
//------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.VoiceCommands;
using Windows.Storage;
 
namespace XiaoMiBackgroundTask
{
    /*
    VoiceCommandServiceConnection 类是接受Cortana传递过来的信息以及给Cortana回应信息的
    里面有几个重要的方法:
        GetVoiceCommandAsync        检索用户的语音命令提交Cortana通过语音或文本。
        ReportFailureAsync          发送一个响应,表明Cortana语音命令处理失败了。
        ReportProgressAsync         发送一个响应,Cortana在处理语音命令。
        ReportSuccessAsync          发送一个响应,Cortana语音命令已成功了。
        RequestAppLaunchAsync       发送一个响应,要求Cortana启动前台应用
        RequestConfirmationAsync    发送一个响应,指示Cortana语音命令需要确认。
        RequestDisambiguationAsync  发送一个响应,表示Cortana语音命令返回多个结果,需要用户选择一个。
    */
 
    public sealed class XiaoMiTask : IBackgroundTask
    {
        BackgroundTaskDeferral _taskDerral;
        VoiceCommandServiceConnection _serviceConnection;
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            _taskDerral = taskInstance.GetDeferral();
            var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
 
            // 验证是否调用了正确的app service
            if (details == null || details.Name != "XiaoMiService")
            {
                _taskDerral.Complete();
                return;
            }
            _serviceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(details);
            // 获取被识别的语音命令
            var cmd = await _serviceConnection.GetVoiceCommandAsync();
            switch (cmd.CommandName)
            {
                case "QueryTrain":
                    var date = cmd.Properties["DateTime"][0];
                    var from = cmd.Properties["From"][0];
                    var to = cmd.Properties["To"][0];
                    await QueryTrain(date, from, to);
                    break;
                case "CancelTrain":
                    var cancelTrain = cmd.Properties["City"][0];
                    CancelTrain(cancelTrain);
                    break;
            }
            _taskDerral.Complete();
        }
 
        private void CancelTrain(string cancelTrain)
        {
            //取消火车 交互类似
            Debug.WriteLine(cancelTrain);
        }
 
        private async Task QueryTrain(string date, string from, string to)
        {
            // msgback是返回给Cortana 要显示的内容
            var msgback = new VoiceCommandUserMessage();
            // msgRepeat是指当cortana对用户语音指令不明确的时候显示的,一般用来消除用户歧义
            // 比如存在让用户选择某个选项时,用户没有按照预期的操作方式去操作,cortana会显示第二消息来告诉一些操作提示信息
            var msgRepeat = new VoiceCommandUserMessage();
 
            //模拟火车列表,真实情况下需要调用api获取火车信息
            var trainList = new List<VoiceCommandContentTile>();
            for (var i = 0; i < 6; i++)
            {
                trainList.Add(new VoiceCommandContentTile
                {
                    AppContext = i, //用来存储该条Tile的标识  一般存储数据id
                    ContentTileType = VoiceCommandContentTileType.TitleWith68x68IconAndText,
                    Image = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Images/300300.jpg")),
                    Title = $"D{i + 3}8{i * 2}",
                    TextLine1 = $"出发:{DateTime.Now.AddHours(i)} - 到达:{DateTime.Now.AddHours(i + 2)}"
                });
            }
 
            TrainList:
 
            msgback.DisplayMessage = msgback.SpokenMessage = $"我找到了{date}从{from}到{to}的火车列表,请选择:";
            msgRepeat.DisplayMessage = msgRepeat.SpokenMessage = "你要告诉我你想预定哪个车次的火车:";
            // 把查询到的火车列表发回到Cortana ,注意 列表最多显示10个
            var response = VoiceCommandResponse.CreateResponseForPrompt(msgback, msgRepeat, trainList);
 
            // 用户选择了哪个项
            var selectedRes = await _serviceConnection.RequestDisambiguationAsync(response);
 
            //创建咨询用户是否确定要预定该车次的信息
            msgback.DisplayMessage = msgback.SpokenMessage = $"您确定要预定 {selectedRes.SelectedItem.Title} 次列车吗?";
            msgRepeat.DisplayMessage = msgRepeat.SpokenMessage = "请选择是或者不是";
            response = VoiceCommandResponse.CreateResponseForPrompt(msgback, msgRepeat);
 
            //返回让用户选择 是 或者 不是 的信息给cortana
            var result = await _serviceConnection.RequestConfirmationAsync(response);
            //如果用户选择是
            if (result.Confirmed)
            {
                //提示预定成功
                msgback.DisplayMessage = msgback.SpokenMessage = $"您成功预定了 {selectedRes.SelectedItem.Title} 次列车,{date}从{from}到{date},{selectedRes.SelectedItem.TextLine1}!";
                msgRepeat.DisplayMessage = msgRepeat.SpokenMessage = $"您成功预定了 {selectedRes.SelectedItem.Title} 次列车。";
                response = VoiceCommandResponse.CreateResponseForPrompt(msgback, msgRepeat);
            }
            else
            {
                goto TrainList;
            }
            // 返回一个操作成功的指令
            await _serviceConnection.ReportSuccessAsync(response);
        }
    }
}

代码意思就不解释了,写的一手详细的注释不是吗?

回到我们的主程序中,在引用里添加该后台应用服务的引用。


注册App后台服务


打开Package.appxmanifest文件,切换到"声明"选项卡,添加一个应用服务的声明,名称随便填,一会VCD文件里要用到这个名字,这里填写XiaoMiService,别以为是小米的意思哈,哥是米黑,是小秘。入口点填写上面创建的后台服务的"命名空间.类名",截图如下:



编写VCD文件

在vcd语音指令中添加两个新的Command节点,以及定义需要的PhraseTopic,对VCD文件不熟悉的移步到:Win10/UWP开发—使用Cortana语音指令与App的前台交互

添加的代码如下:
<Command Name="QueryTrain">
  <Example> 查询去某地的火车  </Example>
  <ListenFor >查询{DateTime}从{From}到{To}的火车</ListenFor>
  <Feedback> 正在查询{DateTime}从{From}到{To}的火车 </Feedback>
  <VoiceCommandService Target="XiaoMiService"/>
</Command>
 
 
<Command Name="CancelTrain">
  <Example> 取消去某地的火车  </Example>
  <ListenFor >取消去{City}的火车</ListenFor>
  <Feedback> 正取消去{City}的火车 </Feedback>
  <VoiceCommandService Target="XiaoMiService"/>
</Command>
 
<!--PhraseTopic 可以提高识别率,内部属性Subject可指定该关键字类型,比如 城市名 姓名  地址 等类型-->
<PhraseTopic Label="City" Scenario="Natural Language">
  <Subject>City/State</Subject>
</PhraseTopic>
<PhraseTopic Label="From" Scenario="Natural Language">
  <Subject>City/State</Subject>
</PhraseTopic>
<PhraseTopic Label="To" Scenario="Natural Language">
  <Subject>City/State</Subject>
</PhraseTopic>
<PhraseTopic Label="DateTime" Scenario="Natural Language">
  <Subject>Date/Time</Subject>
</PhraseTopic>


上面的Command节点中,使用了VoiceCommandService元素来标注该语音指令是要启动后台任务服务,而后台任务服务名称为XiaoMiService。



注册VCD文件


注册vcd文件上篇文章一毛一样:
/// <summary>
/// 注册语音指令
/// </summary>
private async Task InsertVoiceCommands()
{
    await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(
        await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///VoiceCommandsFile.xml")));
}



Ok,打开一次App完成VCD语音指令的注册,然后尽情的使用Cortana吧:Ok,打开一次App完成VCD语音指令的注册,然后尽情的使用Cortana吧:



推荐一个UWP开发群:53078485 大家可以进来一起学习
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值