我在此链接后创建了一个带有进程外后台任务的UWP应用程序
out of process background task
这是我的背景任务
namespace MyTask
{
public sealed class FirstTask : IBackgroundTask
{
private BackgroundTaskDeferral backgroundTaskDeferral;
public void Run(IBackgroundTaskInstance taskInstance)
{
DefaultLaunch();
}
async void DefaultLaunch()
{
// The URI to launch
var uriBing = new Uri("mytempapp://test");
try
{
// Launch the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uriBing); // Throws Error
}
catch (Exception e)
{
}
}
}
}
此后台任务在Timezone Change上激活,工作正常
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder { Name = "FirstTask", TaskEntryPoint = "MyTask.FirstTask" };
taskBuilder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
BackgroundTaskRegistration myFirstTask = taskBuilder.Register();
问题是当调用DefaultLaunch()时抛出错误 . 理想的情况是它应该打开我的注册URI方案的应用程序
System.Runtime.InteropService.COMException(0x80070578) invalid window Handler
This API must be called from a thread with a CC
1 回答
不幸的是, cannot 从后台任务启动了一个URI . 这对用户来说是破坏性的,因为她无法确定应用程序启动的原因和 could be misused easily . 更糟糕的是,从后台线程用户无法 identify which app caused the launch (能够卸载恶意的线程) .
正如错误消息所述,此API可以是 called only from an UI thread - 表示与应用程序视图关联的线程 . 后台任务没有应用程序视图关联,因此无法启动URI .
LaunchFileAsync
和其他Launcher
API也是如此 .