c# - cortana 调用导致启动时崩溃

标签 c# windows-phone-8.1 win-universal-app cortana

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

到目前为止,如果应用程序已经在运行或暂停,我可以激活。但是,如果应用程序完全退出,则在激活后它会立即崩溃。我已经尝试使用 Hockey 和一个简单的“LittleWatson”例程来捕捉崩溃,但它似乎发生得太快而无法被捕捉到。我已经看到一些关于进行私有(private)测试版并尝试获取故障转储的引用资料,但到目前为止我还没有运气。

这是我的激活码在 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);
                }
            }
        }
    }

这是我对命令结果的检查:

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

通过插入消息等,我很确定它在到达这里之前很久就失败了。

我可能缺少一些简单的东西,但我不知道是什么......

是什么导致这么早崩溃?

编辑 我尝试的一件事是使用“不启动调试”配置来 try catch 异常。当我这样做时,应用程序似乎在启动画面上的调试器中永远挂起。然而,这确实让我强行休息了。它卡在

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

据我所知,这只是告诉我该应用程序卡在某处。这是调用堆栈中唯一的一行。

最佳答案

将一段 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];

关于c# - cortana 调用导致启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27975837/

相关文章:

c# - 无效的 Resx 文件。无法加载类型 System.Collections.Generic.List

xaml - VS2015 : "The Windows Software Development Kit (SDK) required by the XAML Designer was not correctly installed." exception with Windows Phone 8. 1 XAML

c# - 如何发送和接收 Windows Phone 8.1 的推送通知

javascript - 如何在 Windows 通用应用程序中设置内容安全策略

c# - proc中的Oracle查询未返回结果

c# - 如果打开自定义错误,是否不会触发 global.asax Application_Error 事件?

c# - 智能卡读卡器插件(已插入卡)事件

c# - 在 UWP 中获取已安装的应用程序列表

c# - Visual Studio 不会构建为 exe (.NET Core 2.0)

sqlite - 使用 SQLite-WinRT 包装器构建问题