Cortana 语音命令无法正常启动应用程序

Cortana voice command do not start app properly

我尝试使用 Cortana 语音命令启动我的应用程序,

但它仅在应用程序暂停时有效。当应用程序关闭时,它会显示 Splashscreen,然后应用程序失败。我无法捕捉到任何异常。

A 与 https://msdn.microsoft.com/en-us/library/dn630430.aspx 和麦克风功能中的代码完全相同。

当语音命令激活应用程序时,将调用此方法,而不会调用 OnLaunched。因此,我们需要与 OnLaunched

中的代码类似的代码
Frame rootFrame = Window.Current.Content as Frame;

if (rootFrame == null)
{
     rootFrame = new Frame();
     rootFrame.CacheSize = 1;
     Window.Current.Content = rootFrame;
     rootFrame.Navigate(typeof(MainPage));
}

使用语音命令打开应用时,您需要在app.OnActivated方法上进行处理:

    protected override void OnActivated(IActivatedEventArgs e)
    {
        // Handle when app is launched by Cortana
        if (e.Kind == ActivationKind.VoiceCommand)
        {
            VoiceCommandActivatedEventArgs commandArgs = e as VoiceCommandActivatedEventArgs;
            SpeechRecognitionResult speechRecognitionResult = commandArgs.Result;

            string voiceCommandName = speechRecognitionResult.RulePath[0];
            string textSpoken = speechRecognitionResult.Text;
            IReadOnlyList<string> recognizedVoiceCommandPhrases;

            System.Diagnostics.Debug.WriteLine("voiceCommandName: " + voiceCommandName);
            System.Diagnostics.Debug.WriteLine("textSpoken: " + textSpoken);

            switch (voiceCommandName)
            ...

如果您需要有关如何集成它的更多信息,请参阅此 link