我正在开发一个UWP应用程序,我想在画布中显示一些数据。但我面临很多问题,现在我无法找出问题所在。 我制作了一个VCD文件,在其中添加了命令,编辑了OnActivated函数,制作了Windows Runtime Component for Background任务,并在Package.appxmanifest文件中进行了声明,但它无法正常工作。 这是VoiceCommandDefinition.xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
<CommandSet xml:lang="en-us" Name="NormalCommands">
<CommandPrefix>in app</CommandPrefix>
<Example>Show data in canvas</Example>
<Command Name="CanvasView">
<Example>test cortana</Example>
<ListenFor RequireAppName="ExplicitlySpecified"> {builtin:Cortinnum}test cortana</ListenFor>
<Feedback>Okay Boss</Feedback>
<VoiceCommandService Target="CanvasViewService"/>
</Command>
</CommandSet>
</VoiceCommands>
以下是App.xaml.cs代码中的OnActivated函数:
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == Windows.ApplicationModel.Activation.ActivationKind.VoiceCommand)
{
var CommandArgs = args as Windows.ApplicationModel.Activation.VoiceCommandActivatedEventArgs;
var result = CommandArgs.Result;
var CommandName = result.RulePath[0];
var text = result.Text;
}
else
base.OnActivated(args);
}
这是后台服务组件的代码:
using System;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.VoiceCommands;
namespace BackgroundService
{
public sealed class service:IBackgroundTask
{
VoiceCommandServiceConnection voiceServiceConnection;
BackgroundTaskDeferral serviceDeferral;
public async void Run(IBackgroundTaskInstance taskInstance)
{
serviceDeferral = taskInstance.GetDeferral();
taskInstance.Canceled += OnTaskCanceled;
var triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
if (triggerDetails != null && triggerDetails.Name == "CanvasView")
{
try
{
voiceServiceConnection =
VoiceCommandServiceConnection.FromAppServiceTriggerDetails(
triggerDetails);
voiceServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;
VoiceCommand voiceCommand = await voiceServiceConnection.GetVoiceCommandAsync();
// perform the appropriate command.
switch (voiceCommand.CommandName)
{
case "CanvasView":
VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
userMessage.DisplayMessage = "Test Data";
userMessage.SpokenMessage = "Test data view successful";
var response = VoiceCommandResponse.CreateResponse(userMessage);
await voiceServiceConnection.ReportSuccessAsync(response);
break;
default:
VoiceCommandUserMessage userMessage1 = new VoiceCommandUserMessage();
userMessage1.DisplayMessage = "Nothing";
userMessage1.SpokenMessage = "NO data to show";
var response1 = VoiceCommandResponse.CreateResponse(userMessage1);
await voiceServiceConnection.ReportSuccessAsync(response1);
break;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Handling Voice Command failed " + ex.ToString());
}
}
}
private void OnVoiceCommandCompleted(VoiceCommandServiceConnection sender, VoiceCommandCompletedEventArgs args)
{
if (this.serviceDeferral != null)
{
this.serviceDeferral.Complete();
}
}
private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
System.Diagnostics.Debug.WriteLine("Task cancelled, clean up");
if (this.serviceDeferral != null)
{
//Complete the service deferral
this.serviceDeferral.Complete();
}
}
}
}
编辑: 以下是Package.appxmanifest中的Extensions标记代码:
<Extensions>
<uap:Extension Category="windows.appService" EntryPoint="Cortinnum.backgroundService.service">
<uap:AppService Name="service" />
</uap:Extension>
</Extensions>