【发布时间】:2016-04-25 06:29:07
【问题描述】:
我有一个 UWP 应用程序,我想在其中添加后台任务支持,以便在我的应用程序处于后台时执行某些操作。 我正在按照这里提到的那样做:https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task
我有一个单独的后台任务项目,在我的包清单文件中,我声明我的应用程序使用后台任务(但因为我使用的是 TimerTrigger,所以是“计时器”任务)。代码:
BackgroundTaskBuilder backgroundTaskBuilder = new BackgroundTaskBuilder { Name = "NotificationUpdater", TaskEntryPoint = "NamespaceOfMyBackgroundTaskInterfaceImplementation.BackgroundTask"};
backgroundTaskBuilder.SetTrigger(new TimeTrigger(15, false));
BackgroundTaskRegistration backgroundTaskRegistration = backgroundTaskBuilder.Register();
现在,当我启动我的应用程序(通过 Visual Studio)并使用 Lifecycle Events 下拉菜单暂停我的应用程序时,它永远不会执行 BackgroundTask 类中的 Run() 方法(IBackgroundTask 接口的实现)。
BackgroundTask 类中的代码:
namespace NamespaceOfMyBackgroundTaskInterfaceImplementation
{
public sealed class BackgroundTask : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
//Code to run in the background
}
}
}
【问题讨论】:
-
您是否在清单文件中声明了一个入口点,在您的主项目中添加了一个引用,并使您的 BTask 成为运行时组件?你也可以看看at the steps here at answer。
-
是的,我已经在清单文件中声明了入口点,在我的应用项目中添加了一个引用。我的 BakgroundTask 实际上是通用 Windows 类库,因为我正在开发 UWP 应用程序。
-
啊,我觉得是我做错了。
-
将其更改为 Windows 运行时组件 并且可能会运行。
-
切换到 Windows 运行时确实有效。非常感谢。
标签: win-universal-app background-task