Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cortana invocation causes crash at startup

I have a Windows Phone 8.1 Universal App that I am working on adding basic Cortana support to. A lot of the articles about this are for Silverlight etc. - I'm finding it hard to find really good information about this.

So far, I have activation working if the app is already running or suspended. However, if the app is completely exited, then upon activation it crashes immediately. I've tried using Hockey and a simple "LittleWatson" routine to catch the crash, but it seems to happen too soon to be caught. I've seen some references to doing a private beta and trying to get the crash dump, but I didn't have any luck with that so far.

Here's what my activation code looks like in 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);
                }
            }
        }
    }

and here is my check for the command result:

    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;
    }

I'm pretty sure from inserting messages etc. that it fails long before it gets this far.

There's likely something easy I'm missing but I don't know what...

What is causing the crash so early?

EDIT One thing I tried is using the "debug without launch" configuration to try to catch the exception. When I do this, the app appears to hang forever connected in the debugger on the splash screen. However, that did let me force a break. It hangs in

global::Windows.UI.Xaml.Application.Start((p) => new App());

which as best I can tell, just tells me the app is hanging somewhere. That's the only line in the call stack.

like image 762
MikeBaz - MSFT Avatar asked Jan 16 '15 01:01

MikeBaz - MSFT


1 Answers

Copy a segment of the OnLaunched code into OnActivated like in the example below. OnLaunched is not called when the App is Activated and it does some essential work like activating the window.

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];
like image 74
Lucio Avatar answered Nov 14 '22 21:11

Lucio