我有一个Windows Phone 8.1通用应用程序,我正在努力添加基本的Cortana支持。关于这一点的很多文章都是针对Silverlight等的 - 我发现很难找到关于此的真正好的信息。
到目前为止,如果应用程序已在运行或暂停,我可以激活。但是,如果应用程序已完全退出,则在激活时它会立即崩溃。我曾经尝试过使用曲棍球和简单的小魔鬼"例行程序,以赶上崩溃,但它似乎发生得太快被抓住。我已经看到了一些私人测试版的参考资料并试图获得崩溃转储,但到目前为止我还没有运气。
以下是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;
}
我非常确定在插入消息之后,它很久就会失败。
我可能很容易失踪,但我不知道是什么......
这么早造成撞击的原因是什么?
编辑我尝试的一件事是使用"调试而不启动"尝试捕获异常的配置。当我这样做时,应用程序似乎在启动屏幕上的调试器中永久挂起。然而,这确实让我强行休息。它挂在
global::Windows.UI.Xaml.Application.Start((p) => new App());
我尽力告诉我,只是告诉我应用程序挂在某个地方。这是调用堆栈中唯一的一行。
答案 0 :(得分: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];