【发布时间】:2015-06-30 19:33:48
【问题描述】:
好的,我有一个支持 Cortana 的应用程序。 我有一些命令可以导航到特定页面。但在某些情况下,我只想提供回复(文本或 Cortana 发音),而不是启动整个应用程序。有什么办法吗?
例如,当你问 Cortana“美国的首都是什么?”,她只会回复你“华盛顿”。我想做这样的事情。
【问题讨论】:
标签: c# windows-runtime windows-phone-8.1 cortana
好的,我有一个支持 Cortana 的应用程序。 我有一些命令可以导航到特定页面。但在某些情况下,我只想提供回复(文本或 Cortana 发音),而不是启动整个应用程序。有什么办法吗?
例如,当你问 Cortana“美国的首都是什么?”,她只会回复你“华盛顿”。我想做这样的事情。
【问题讨论】:
标签: c# windows-runtime windows-phone-8.1 cortana
看看这个tutorial的底部,它详细说明了如何设置她所说和显示的内容。
基本上,您需要有一个通过语音命令执行的后台任务,然后在执行期间您需要创建一个VoiceCommandUserMessage。
使用taskInstance.TriggerDetails获取VoiceServiceConnection
voiceServiceConnection =
VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
triggerDetails);
等待语音命令完成
VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
然后添加
VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
userMessage.DisplayMessage = "Here’s your trip.";
userMessage.SpokenMessage = "Your trip to Vegas is on August 3rd.";
将其包装在响应中
var response =
VoiceCommandResponse.CreateResponse(
userMessage);
最后,让 Cortana 显示它:
await voiceServiceConnection.ReportSuccessAsync(response);
【讨论】:
据我了解,这仅适用于 Windows 10 应用程序。
【讨论】:
Windows 应用程序允许您在前台和后台使用 Cortana 进行迭代。我相信背景正是您要寻找的:
首先您需要创建一个 XML 文件来表示您的 VCD(语音命令定义)。这是您声明要使用的命令的地方:
<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
<CommandSet xml:lang="en-us" Name="HomeControlCommandSet_en-us">
<CommandPrefix>HomeControl</CommandPrefix>
<Example>Control alarm, temperature, light and others</Example>
<Command Name="CheckTemperature">
<Example>Check temperature</Example>
<ListenFor>check [current] temperature</ListenFor>
<Feedback>Checking temperature</Feedback>
<VoiceCommandService Target="VoiceCommandService" />
</Command>
</CommandSet>
</VoiceCommands>
需要在App.xaml.cs里面的App.OnLaunched注册VCD后:
protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
...
// Install the VCD
try
{
StorageFile vcdStorageFile = await Package.Current.InstalledLocation.GetFileAsync(@"HomeControlCommands.xml");
await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("There was an error registering the Voice Command Definitions", ex);
}
}
之后,需要新建一个Windows Runtime Component项目,并创建一个实现IBackgroundTask的类:
public sealed class HomeControlVoiceCommandService : IBackgroundTask
{
private VoiceCommandServiceConnection voiceServiceConnection;
private BackgroundTaskDeferral serviceDeferral;
public async void Run(IBackgroundTaskInstance taskInstance)
{
// Create the deferral by requesting it from the task instance
serviceDeferral = taskInstance.GetDeferral();
AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
if (triggerDetails != null && triggerDetails.Name.Equals("VoiceCommandService"))
{
voiceServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
// Perform the appropriate command depending on the operation defined in VCD
switch (voiceCommand.CommandName)
{
case "CheckTemperature":
VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
userMessage.DisplayMessage = "The current temperature is 23 degrees";
userMessage.SpokenMessage = "The current temperature is 23 degrees";
VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage, null);
await voiceServiceConnection.ReportSuccessAsync(response);
break;
default:
break;
}
}
// Once the asynchronous method(s) are done, close the deferral
serviceDeferral.Complete();
}
}
不要忘记添加这个新项目作为主项目的参考。另外,您需要在 Package.appxmanifest 中注册服务:
<Extensions>
<uap:Extension Category="windows.appService" EntryPoint="CortanaComponent.HomeControlVoiceCommandService">
<uap:AppService Name="VoiceCommandService" />
</uap:Extension>
</Extensions>
这应该可行!
如需完整指南,您可以查看post
【讨论】: