C# Cortana后台任务在移动设备上崩溃,但在桌面上却能完美工作
在过去的几天里,我一直在研究这个问题,但我还没有找到解决方案。下面的代码基于上的CortanaVoiceCommand示例,该代码在桌面上运行良好,但每次移动设备上的后台任务都会崩溃 在ONTASKCancelled中,BackgroundTaskCancellationReason总是报告为系统策略,但我还没有找到解决方案 感谢任何人提供的帮助C# Cortana后台任务在移动设备上崩溃,但在桌面上却能完美工作,c#,uwp,cortana,C#,Uwp,Cortana,在过去的几天里,我一直在研究这个问题,但我还没有找到解决方案。下面的代码基于上的CortanaVoiceCommand示例,该代码在桌面上运行良好,但每次移动设备上的后台任务都会崩溃 在ONTASKCancelled中,BackgroundTaskCancellationReason总是报告为系统策略,但我还没有找到解决方案 感谢任何人提供的帮助 using System; using System.Collections; using System.Collections.ObjectMode
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.ApplicationModel.VoiceCommands;
namespace TramTimesVoice
{
public sealed class VoiceCommandService : IBackgroundTask
{
private BackgroundTaskDeferral backgroundTaskDeferral;
private IEnumerable services;
private Service service;
private Stop stop;
private VoiceCommandServiceConnection voiceCommandServiceConnection;
public async void Run(IBackgroundTaskInstance taskInstance)
{
backgroundTaskDeferral = taskInstance.GetDeferral();
taskInstance.Canceled += OnTaskCanceled;
AppServiceTriggerDetails triggerDetails = taskInstance.TriggerDetails as AppServiceTriggerDetails;
if (triggerDetails != null && triggerDetails.Name == "VoiceCommandService")
{
try
{
voiceCommandServiceConnection = VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
voiceCommandServiceConnection.VoiceCommandCompleted += OnVoiceCommandCompleted;
VoiceCommand voiceCommand = await voiceCommandServiceConnection.GetVoiceCommandAsync();
if (voiceCommand.CommandName == "getServices")
{
await SendCompletionMessageForServices(voiceCommand.Properties["stop"][0]);
}
else
{
LaunchAppInForeground();
}
}
catch (Exception ex)
{
throw new Exception("Handling Voice Command Failed: " + ex.ToString());
}
}
}
private async void LaunchAppInForeground()
{
VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
userMessage.SpokenMessage = "Launching Tram Times";
VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage);
response.AppLaunchArgument = "";
await voiceCommandServiceConnection.RequestAppLaunchAsync(response);
}
private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
if (backgroundTaskDeferral != null)
{
backgroundTaskDeferral.Complete();
}
}
private void OnVoiceCommandCompleted(VoiceCommandServiceConnection sender, VoiceCommandCompletedEventArgs args)
{
if (backgroundTaskDeferral != null)
{
backgroundTaskDeferral.Complete();
}
}
private async Task SendCompletionMessageForServices(String input)
{
await ShowProgressScreen("Working On It");
IEnumerable stops = new Stops().Get();
foreach (Stop item in stops)
{
if (input == item.Voice)
{
stop = new Stop()
{
Code = item.Code,
Name = item.Name,
Latitude = item.Latitude,
Longitude = item.Longitude,
ApiCodes = item.ApiCodes,
City = item.City,
Lines = item.Lines,
Voice = item.Voice
};
break;
}
}
if (stop.City == "Blackpool")
{
services = await new Blackpool().Get(stop.ApiCodes);
}
else if (stop.City == "Edinburgh")
{
services = await new Edinburgh().Get(stop.ApiCodes);
}
else if (stop.City == "London")
{
services = await new London().Get(stop.ApiCodes);
}
else if (stop.City == "Manchester")
{
services = await new Manchester().Get(stop.ApiCodes);
}
else if (stop.City == "Midland")
{
services = await new Midland().Get(stop.ApiCodes);
}
else if (stop.City == "Nottingham")
{
services = await new Nottingham().Get(stop.ApiCodes);
}
else if (stop.City == "Sheffield")
{
services = await new Sheffield().Get(stop.ApiCodes);
}
ObservableCollection<Service> data = new ObservableCollection<Service>();
foreach (Service item in services)
{
if (item.DestinationScheduled != null)
{
service = new Service()
{
Destination = item.Destination,
DestinationRealtime = item.DestinationRealtime,
DestinationRealtimeFull = item.DestinationRealtimeFull,
DestinationScheduled = item.DestinationScheduled,
DestinationScheduledFull = item.DestinationScheduledFull
};
data.Add(service);
}
else
{
service = new Service()
{
Destination = item.Destination,
DestinationRealtime = item.DestinationRealtime,
DestinationRealtimeFull = item.DestinationRealtimeFull,
DestinationScheduled = item.DestinationRealtime,
DestinationScheduledFull = item.DestinationRealtimeFull
};
data.Add(service);
}
}
data = new ObservableCollection<Service>(data.OrderBy(service => service.DestinationScheduledFull).ThenBy(service => service.Destination));
VoiceCommandUserMessage userMessage = new VoiceCommandUserMessage();
userMessage.DisplayMessage = "I Found These Services";
userMessage.SpokenMessage = "I found these services from " + stop.Voice + ". ";
Collection<VoiceCommandContentTile> tiles = new Collection<VoiceCommandContentTile>();
VoiceCommandContentTile tile = new VoiceCommandContentTile();
tile.AppLaunchArgument = stop.Name;
tile.ContentTileType = VoiceCommandContentTileType.TitleWithText;
tile.Title = stop.Name;
if (data.Count > 0)
{
if (data[0].DestinationRealtime != null)
{
tile.TextLine1 = data[0].DestinationRealtime + " " + data[0].Destination;
userMessage.SpokenMessage += data[0].DestinationRealtime + " to " + data[0].Destination + ". ";
}
else
{
tile.TextLine1 = data[0].DestinationScheduled + " " + data[0].Destination;
userMessage.SpokenMessage += data[0].DestinationScheduled + " to " + data[0].Destination + ". ";
}
}
if (data.Count > 1)
{
if (data[1].DestinationRealtime != null)
{
tile.TextLine2 = data[1].DestinationRealtime + " " + data[1].Destination;
userMessage.SpokenMessage += data[1].DestinationRealtime + " to " + data[1].Destination + ". ";
}
else
{
tile.TextLine2 = data[1].DestinationScheduled + " " + data[1].Destination;
userMessage.SpokenMessage += data[1].DestinationScheduled + " to " + data[1].Destination + ". ";
}
}
if (data.Count > 2)
{
if (data[2].DestinationRealtime != null)
{
tile.TextLine3 = data[2].DestinationRealtime + " " + data[2].Destination;
userMessage.SpokenMessage += data[2].DestinationRealtime + " to " + data[2].Destination + ". ";
}
else
{
tile.TextLine3 = data[2].DestinationScheduled + " " + data[2].Destination;
userMessage.SpokenMessage += data[2].DestinationScheduled + " to " + data[2].Destination + ". ";
}
}
tiles.Add(tile);
VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userMessage, tiles);
response.AppLaunchArgument = "";
await voiceCommandServiceConnection.ReportSuccessAsync(response);
}
private async Task ShowProgressScreen(String message)
{
var userProgressMessage = new VoiceCommandUserMessage();
userProgressMessage.DisplayMessage = message;
userProgressMessage.SpokenMessage = message;
VoiceCommandResponse response = VoiceCommandResponse.CreateResponse(userProgressMessage);
await voiceCommandServiceConnection.ReportProgressAsync(response);
}
}
}
使用系统;
使用系统集合;
使用System.Collections.ObjectModel;
使用System.Linq;
使用System.Threading.Tasks;
使用Windows.ApplicationModel.AppService;
使用Windows.ApplicationModel.Background;
使用Windows.ApplicationModel.VoiceCommand;
名称空间语音
{
公共密封类VoiceCommandService:IBackgroundTask
{
私人背景任务延期背景任务延期;
私人服务;
私人服务;
私人停车场;
专用VoiceCommandServiceConnection VoiceCommandServiceConnection;
公共异步无效运行(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeleral=taskInstance.getDeleral();
taskInstance.Cancelled+=ontaskCancelled;
AppServiceTriggerDetails triggerDetails=taskInstance.triggerDetails作为AppServiceTriggerDetails;
if(triggerDetails!=null&&triggerDetails.Name==“VoiceCommandService”)
{
尝试
{
voiceCommandServiceConnection=voiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
voiceCommandServiceConnection.VoiceCommandCompleted+=OnVoiceCommandCompleted;
VoiceCommand VoiceCommand=等待voiceCommandServiceConnection.GetVoiceCommandAsync();
如果(voiceCommand.CommandName==“getServices”)
{
等待SendCompletionMessageForServices(voiceCommand.Properties[“stop”][0]);
}
其他的
{
启动AppInformeGround();
}
}
捕获(例外情况除外)
{
抛出新异常(“处理语音命令失败:+ex.ToString());
}
}
}
私有异步void launchAppInformeGround()
{
VoiceCommandUserMessage userMessage=新的VoiceCommandUserMessage();
userMessage.SpokenMessage=“启动电车时间”;
VoiceCommandResponse=VoiceCommandResponse.CreateResponse(userMessage);
response.AppLaunchArgument=“”;
等待voiceCommandServiceConnection.RequestApplaunCAsync(响应);
}
私有无效OnTaskCanceled(IBackgroundTaskInstance发件人,BackgroundTaskCancellationReason)
{
if(BackgroundTaskDeleral!=null)
{
BackgroundTaskDeleral.Complete();
}
}
VoiceCommandCompleted上的私有无效(VoiceCommandServiceConnection发送方,VoiceCommandCompletedEventArgs args args)
{
if(BackgroundTaskDeleral!=null)
{
BackgroundTaskDeleral.Complete();
}
}
专用异步任务SendCompletionMessageForServices(字符串输入)
{
等待ShowProgressScreen(“正在处理”);
IEnumerable stops=new stops().Get();
foreach(站点中的停止项目)
{
如果(输入==项.语音)
{
停止=新停止()
{
代码=项目。代码,
名称=项。名称,
纬度=项目。纬度,
经度=项目经度,
ApiCodes=item.ApiCodes,
城市=项目。城市,
行=项。行,
语音=项目。语音
};
打破
}
}
如果(stop.City==“Blackpool”)
{
services=wait new Blackpool().Get(stop.ApiCodes);
}
否则如果(stop.City==“爱丁堡”)
{
services=wait new Edinburgh().Get(stop.ApiCodes);
}
否则如果(stop.City==“伦敦”)
{
services=wait new London().Get(stop.ApiCodes);
}
否则如果(stop.City==“曼彻斯特”)
{
services=wait new Manchester().Get(stop.ApiCodes);
}
否则如果(stop.City==“米德兰”)
{
services=wait new Midland().Get(stop.ApiCodes);
}
否则如果(stop.City==“诺丁汉”)
{
服务=等待新诺丁汉().Get(stop.ApiCodes);
}
否则如果(stop.City==“谢菲尔德”)
{
services=wait new Sheffield().Get(stop.ApiCodes);
}
ObservableCollection数据=新的ObservableCollection();
foreach(服务中的服务项)
{
if(item.DestinationScheduled!=null)
{
服务=新服务()
{
目的地=项目。目的地,
DestinationRealtime=item.DestinationRealtime,
DestinationRealtimeFull=item.DestinationRealtimeFull,
DestinationsScheduled=项目。DestinationsScheduled,
DestinationsScheduledFull=项目。DestinationsScheduledFull
};
数据添加(服务);
}
其他的
{
服务=新服务()