在我的应用程序中使用Cortana

时间:2018-07-10 12:06:28

标签: c# uwp cortana

使用Cortana来控制我的应用程序时遇到一些问题。 我可以使用前台命令来控制我的应用程序,但是不能使用后台命令来控制我的应用程序。 我的Package.appxmainfest

                        

我的VCD文件

    <?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">

  <CommandSet xml:lang="zh-cn" Name="set">
    <AppName>随机抽号器</AppName>
  <Example>抽号 或 随机抽号</Example>

    <Command Name="Generate">
      <Example>随机抽号</Example>
      <ListenFor>[随机]抽号</ListenFor>
      <Feedback>正在为您抽取号码……</Feedback>
      <VoiceCommandService Target="RNGCortanaService"/>
    </Command>

    <Command Name="In-AppGenerate">
      <Example>随机抽号</Example>
      <ListenFor>[在]应用内[随机]抽号</ListenFor>
      <Feedback>正在为您抽取号码……</Feedback>
      <Navigate/>
    </Command>
  </CommandSet>
</VoiceCommands>

我的后台服务

namespace RandomNumberGenerationCortanaService
{
    public sealed class RNGCortanaService : IBackgroundTask
    {
        BackgroundTaskDeferral serviceDeferral;
        VoiceCommandServiceConnection voiceServiceConnection;
        ResourceMap cortanaResourceMap;
        ResourceContext cortanaContext;

        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            serviceDeferral = taskInstance.GetDeferral();

            taskInstance.Canceled += OnTaskCanceled;

            var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;

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

                    voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;

                    VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();

                    // perform the appropriate command.
                    switch (voiceCommand.CommandName)
                    {
                        case "Generate":
                            await Generate();
                            break;
                        default:
                            break;
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
                }
            }
        }

        private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            System.Diagnostics.Debug.WriteLine("任务取消");
            if (this.serviceDeferral != null)
            {
                //Complete the service deferral
                this.serviceDeferral.Complete();
            }
        }

        private void OnVoiceCommandCompleted(VoiceCommandServiceConnection sender, VoiceCommandCompletedEventArgs args)
        {
            if (this.serviceDeferral != null)
            {
                this.serviceDeferral.Complete();
            }
        }

        private async Task Generate()
        {
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
            StorageFile sampleFile = await storageFolder.GetFileAsync("settings.txt");
            string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
            var settings = text.Split(',');
            bool convert = true;
            convert = int.TryParse(settings[0], out int qs);
            convert = int.TryParse(settings[1], out int zz);
            convert = int.TryParse(settings[2], out int n);
            int[] results = new int[n];
            //根据起始号和终止号设置数组,长度为zz-qs+1,将0与起始号对齐
            int[] numbers = new int[zz - qs + 1];
            int i = 1; //i表示正在生成的随机数的序号
            while (i <= n)
            {
                //产生[0,zz-qs]的随机数
                var a = new Random(Guid.NewGuid().GetHashCode()).Next(0, zz - qs + 1);
                if (numbers[a] == 0)
                {
                    //将0与起始号对齐
                    numbers[a] = 1; results[i - 1] = a + qs; i++;
                }
            }
            Array.Sort(results);
            var msgback = new VoiceCommandUserMessage(); var msgRepeat = new VoiceCommandUserMessage();
            var spokenMessage = "抽号结果为:";
            foreach (int aresult in results)
            {
                spokenMessage = spokenMessage + aresult.ToString() + "号";
            }
            msgback.DisplayMessage = msgback.SpokenMessage = spokenMessage;
            msgRepeat.DisplayMessage = msgRepeat.SpokenMessage = spokenMessage;
            var response = VoiceCommandResponse.CreateResponseForPrompt(msgback, msgRepeat);
            await voiceServiceConnection.ReportSuccessAsync(response);
        }

    }
}

使用后台命令后,我看到了这一点。 enter image description here

这意味着“有一些问题,请稍后再试”

我想知道如何解决此问题并使用后台命令正确控制我的应用程序。

0 个答案:

没有答案