BackgroundTaskDeferral.Complete 在应用程序 运行 未在 UWP 应用程序中调试时不起作用

BackgroundTaskDeferral.Complete does not work when application run without debugging in UWP app

BackgroundTaskDeferral.Complete 应用程序调试运行时正常工作,即使应用程序挂起,主项目也可以捕获 OnComplete 事件。

但是当应用程序在没有调试的情况下运行并且应用程序进入后台时,BackgroundTaskDeferral.Complete 不起作用。后台任务调用时主项目无法接收到任何东西(当主项目捕获事件 OnComplete 时我会显示 toast 通知)

BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
_deferral.Complete();

如果我的理解是正确的,问题是您的主项目没有收到 后台任务 完成其工作的通知。这很正常 - 您的主要项目 is being suspended by the OS, shortly after your app goes to background

它在您调试时工作,因此 PLM 被禁用并且 suspending/resuming events are not being raised

如果你想在后台执行一些工作 - 把它放在后台任务中,发送 toast 通知应该可以正常工作。