cortana调用导致启动时崩溃

Mik*_*SFT 5 c# windows-phone-8.1 win-universal-app cortana

我有一个Windows Phone 8.1通用应用程序,我正在努力添加基本的Cortana支持.很多关于这个的文章都是针对Silverlight等的 - 我发现很难找到关于这个的真正好的信息.

到目前为止,如果应用已经运行或暂停,我有激活工作.但是,如果应用程序完全退出,则在激活时它会立即崩溃.我已经尝试过使用Hockey和一个简单的"LittleWatson"例程来捕捉崩溃,但似乎发生得太快就被抓住了.我已经看到一些引用私有测试版并试图获得崩溃转储,但到目前为止我没有任何运气.

这是我的激活码的样子app.xaml.cs:

    protected override void OnActivated(IActivatedEventArgs args) {
        base.OnActivated(args);
        ReceivedSpeechRecognitionResult = null;
        if (args.Kind == ActivationKind.VoiceCommand) {
            var commandArgs = args as VoiceCommandActivatedEventArgs;
            if (commandArgs != null) {
                ReceivedSpeechRecognitionResult = commandArgs.Result;
                var rootFrame = Window.Current.Content as Frame;
                if (rootFrame != null) {
                    rootFrame.Navigate(typeof(CheckCredentials), null);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我检查命令结果:

    private async Task CheckForVoiceCommands() {
        await Task.Delay(1); // not sure why I need this
        var speechRecognitionResult = ((App)Application.Current).ReceivedSpeechRecognitionResult;
        if (speechRecognitionResult == null) {
            return;
        }
        var voiceCommandName = speechRecognitionResult.RulePath[0];

        switch (voiceCommandName) {
            // omitted
        }

        ((App)Application.Current).ReceivedSpeechRecognitionResult = null;
    }
Run Code Online (Sandbox Code Playgroud)

我很确定通过插入消息等它很久就会失败.

我可能很容易丢失,但我不知道是什么......

是什么导致这么大的崩溃?

编辑我试过的一件事是使用"debug without launch"配置来尝试捕获异常.当我这样做时,应用程序似乎在启动屏幕上的调试器中永久挂起.然而,这确实让我强行休息.它挂起来了

global::Windows.UI.Xaml.Application.Start((p) => new App());
Run Code Online (Sandbox Code Playgroud)

我尽力告诉我,只是告诉我应用程序挂在某处.这是调用堆栈中唯一的一行.

小智 10

将OnLaunched代码的一部分复制到OnActivated,如下例所示.当应用程序被激活时,不会调用OnLaunched,它会执行一些必要的工作,如激活窗口.

protected override void OnActivated(IActivatedEventArgs args)
{
    // When a Voice Command activates the app, this method is going to 
    // be called and OnLaunched is not. Because of that we need similar
    // code to the code we have in 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));
    }
 
    Window.Current.Activate();
 
    // For VoiceCommand activations, the activation Kind is ActivationKind.VoiceCommand
    if(args.Kind == ActivationKind.VoiceCommand)
    { 
        // since we know this is the kind, a cast will work fine
        VoiceCommandActivatedEventArgs vcArgs = (VoiceCommandActivatedEventArgs)args;
 
        // The NavigationTarget retrieved here is the value of the Target attribute in the
        // Voice Command Definition xml Navigate node
        string target = vcArgs.Result.SemanticInterpretation.Properties["NavigationTarget"][0];
Run Code Online (Sandbox Code Playgroud)