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

时间:2015-02-13 12:09:13

标签: c# windows-phone-8.1 cortana

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

但仅在应用暂停时才有效。当应用关闭时,它会显示启动画面,然后应用失败。我无法发现任何异常。

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

2 个答案:

答案 0 :(得分:0)

当语音命令激活应用程序时,将调用此方法,而不会调用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));
}

答案 1 :(得分:0)

使用语音指令打开应用程序时,您需要在 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