Cortana 回复而不是应用程序启动

Cortana Reply instead of App Launching

好的,我有一个支持 Cortana 的应用程序。 我有一些命令可以导航到特定页面。但在某些情况下,我只想提供回复(文本或由 Cortana 发音)而不是启动整个应用程序。有什么办法吗?

例如,当您询问 Cortana "What is capital of USA?" 时,她只会回复您 "Washington"。我想做这样的事情。

看看这个 tutorial 的底部,它详细说明了如何设置她所说和显示的内容。

基本上你需要有一个从语音命令运行的后台任务,然后在执行期间你需要创建一个 VoiceCommandUserMessage.

使用 taskInstance.TriggerDetails

获取 VoiceServiceConnection
    voiceServiceConnection = 
      VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
        triggerDetails); 

等待语音命令完成

    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

然后添加

VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
userMessage.DisplayMessage = "Here’s your trip.";
userMessage.SpokenMessage = "Your trip to Vegas is on August 3rd.";

将其包装在响应中

var response = 
  VoiceCommandResponse.CreateResponse(
    userMessage);

最后,让Cortana显示出来:

await voiceServiceConnection.ReportSuccessAsync(response);

也看看 design guidelines for cortana

据我了解,这仅适用于 Windows 10 个应用程序。

https://channel9.msdn.com/Events/Build/2015/2-691

Windows 应用程序允许您在前台和后台与 Cortana 交互。我相信背景就是你要找的:

首先您需要创建一个 XML 文件来重新设置您的 VCD(语音命令定义)。这是您声明要使用的命令的地方:

<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
  <CommandSet xml:lang="en-us" Name="HomeControlCommandSet_en-us">
    <CommandPrefix>HomeControl</CommandPrefix>
    <Example>Control alarm, temperature, light and others</Example>

    <Command Name="CheckTemperature">
      <Example>Check temperature</Example>
      <ListenFor>check [current] temperature</ListenFor>
      <Feedback>Checking temperature</Feedback>
      <VoiceCommandService Target="VoiceCommandService" />
    </Command>

  </CommandSet>
</VoiceCommands>

在App.xaml.cs里面App.OnLaunched注册VCD后:

protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
    ...
    // Install the VCD
    try
    {
        StorageFile vcdStorageFile = await Package.Current.InstalledLocation.GetFileAsync(@"HomeControlCommands.xml");
        await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("There was an error registering the Voice Command Definitions", ex);
    }
}

之后,您需要创建一个新的 Windows Runtime Component 项目并创建一个 class 实现 IBackgroundTask:

public sealed class HomeControlVoiceCommandService : IBackgroundTask
{
    private VoiceCommandServiceConnection voiceServiceConnection;
    private BackgroundTaskDeferral serviceDeferral;

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        // Create the deferral by requesting it from the task instance
        serviceDeferral = taskInstance.GetDeferral();

        AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

        if (triggerDetails != null && triggerDetails.Name.Equals("VoiceCommandService"))
        {
            voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);

            VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

            // Perform the appropriate command depending on the operation defined in VCD
            switch (voiceCommand.CommandName)
            {
                case "CheckTemperature":
                    VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
                    userMessage.DisplayMessage = "The current temperature is 23 degrees";
                    userMessage.SpokenMessage = "The current temperature is 23 degrees";

                    VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage, null);
                    await voiceServiceConnection.ReportSuccessAsync(response);
                    break;

                default:
                    break;
            }
        }

        // Once the asynchronous method(s) are done, close the deferral
        serviceDeferral.Complete();
    }
}

不要忘记添加这个新项目作为主要项目的参考。此外,您需要在 Package.appxmanifest:

中注册服务
<Extensions>
<uap:Extension Category="windows.appService" EntryPoint="CortanaComponent.HomeControlVoiceCommandService">
  <uap:AppService Name="VoiceCommandService" />
</uap:Extension>
</Extensions>

这应该有效!

如需完整指南,您可以查看此post