我想實現柯塔娜到我的應用程序,所以當用戶說「嘿柯塔娜,在測試應用程式閱讀一步一個」應用程序將讀取一個列表框的第一個元素。我遇到的問題是,當我說這個時,應用程序只是用我說的句子打開一個bing網頁。UWP柯塔娜不登記應用命令
<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">
<CommandSet xml:lang="en-US" Name="CommandSet_en-US">
<CommandPrefix> Test App</CommandPrefix>
<Example> read step one </Example>
<Command Name="step">
<Example> read step one </Example>
<ListenFor> read step {number} </ListenFor>
<Feedback> Reading Instruction </Feedback>
<Navigate Target="Page1"/>
</Command>
<PhraseList Label= "number">
<Item> one </Item>
<Item> two </Item>
</PhraseList>
</CommandSet>
</VoiceCommands>
在app.xaml.cs:
protected async override void OnLaunched(LaunchActivatedEventArgs e)
//code
Window.Current.Activate();
}
try
{
StorageFile vcdStorageFile = await Package.Current.InstalledLocation.GetFileAsync(@"CortanaCommands.xml");
await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("There was an error Registering the Voice Command Definitions", ex);
}
}
protected override void OnActivated(IActivatedEventArgs e)
{
if (e.Kind != Windows.ApplicationModel.Activation.ActivationKind.VoiceCommand)
{
return;
}
var commandArgs = e as Windows.ApplicationModel.Activation.VoiceCommandActivatedEventArgs;
var speechRecognitionResult = commandArgs.Result;
string voiceCommandName = speechRecognitionResult.RulePath[0];
string textSpoken = speechRecognitionResult.Text;
string spokenStep = "";
try
{
spokenStep = speechRecognitionResult.SemanticInterpretation.Properties["number"][0];
}
catch
{
}
Frame rootFrame = Window.Current.Content as Frame;
MainPage page = rootFrame.Content as MainPage;
if (page == null)
{
return;
}
switch (spokenStep)
{
case "one":
page.Count1 = 0;
break;
default:
//no match
break;
}
switch (voiceCommandName)
{
case "step":
page.StepOne();
break;
default:
break;
}
}
在代碼中,你捕捉異常,如果有註冊的語音命令定義一個錯誤,你確定你已經成功註冊了你的vcd文件?你的代碼在我身邊工作的很好,cortana會在短時間內啓動應用程序,然後關閉它,因爲你錯過了'OnActivated(IActivatedEventArgs e)'方法末尾的'Window.Current.Activate();',或者你打算這麼做嗎? –
您好我設法讓Cortana實際上識別應用程序,但是當它到達線頁面.stepOne()應用程序崩潰爲「對象引用未設置爲對象的實例」。頁面值爲null,但類型爲App2.Page1。 – myf33tsmell
在我身邊,頁面值是'MainPage',而它是'Page1'在你身邊,但忘記了這一點,在你的代碼中,如果'page'爲null,它將返回,這個錯誤不應該發生。問題實際上是在你的App2.Page1的StepOne()方法中,而且我沒有測試這個方法,這個錯誤依然不會發生在我身邊,請發佈這個代碼方法。 –