【问题标题】:Cortana Reply instead of App LaunchingCortana 回复而不是应用程序启动
【发布时间】:2015-06-30 19:33:48
【问题描述】:

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

例如,当你问 Cortana“美国的首都是什么?”,她只会回复你“华盛顿”。我想做这样的事情。

【问题讨论】:

    标签: c# windows-runtime windows-phone-8.1 cortana


    【解决方案1】:

    看看这个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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 你是对的。要做这样的事情,你必须创建 Windows.ApplicationModel.AppService -> 最低支持是 Windows 10(移动和桌面)。
      • 正确,仅限 Windows 10。
      【解决方案3】:

      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项目,并创建一个实现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

      【讨论】:

        猜你喜欢
        • 2013-11-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-28
        • 1970-01-01
        • 2016-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多