以下是获取搜索词的方法,而不是从值列表中获取。这适用于 HTML/JS,而不是 XAML,但应该给你一个好主意。
XML
<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
<CommandSet xml:lang="en-us" Name="FNOW_en-us">
<AppName> YourAppName </AppName>
<Example> Search books </Example>
<Command Name="SearchFor">
<Example> Search for Harry Potter </Example>
<ListenFor RequireAppName="BeforeOrAfterPhrase"> find book {SearchTerm} </ListenFor>
<Feedback> Searching books </Feedback>
<Navigate />
</Command>
<PhraseTopic Label="SearchTerm" Scenario="Search"/>
</CommandSet>
</VoiceCommands>
JavaScript
function handleVoiceCommand(args) {
var command,
commandResult,
commandName = '',
commandProperties = {};
if (args.detail && args.detail.detail) {
command = args.detail.detail[0];
if (command) {
commandResult = command.result;
if (commandResult) {
if (commandResult.rulePath) {
commandName = commandResult.rulePath[0];
}
if (commandResult.semanticInterpretation) {
commandProperties = commandResult.semanticInterpretation.properties;
}
// Act on the different commands.
// Command Names are defined within VoiceCommands.xml.
switch(commandName) {
case 'SearchFor':
console.log('Cortana Command: SearchFor');
console.log('Search Term: ' + commandProperties.SearchTerm[0]);
break;
}
}
}
}
}
WinJS.Application.addEventListener('activated', function (args) {
var appLaunchVoiceCommand;
appLaunchVoiceCommand = Windows.ApplicationModel.Activation.ActivationKind.voiceCommand || 16;
if (args.detail.kind === appLaunchVoiceCommand) {
return handleVoiceCommand(args);
}
});
WinJS.Application.start();