首页 文章

后台任务不在UWP中运行

提问于
浏览
0

我有一个UWP应用程序,我想在我的应用程序处于后台时为某些事情添加后台任务支持 . 我完全按照这里提到的那样做:https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task

我有一个单独的项目用于后台任务,在我的包清单文件中,我已声明我的应用程序使用后台任务(但是“timer”任务,因为我使用的是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
        }
    }
}

1 回答

  • 1

    正如在谈话中发现的那样,问题在于项目类型的错误声明 . 应该是Windows运行时组件 .

    对于粉丝,请看一下at this answer,其中介绍了这些步骤 . 它回答了Silverlight 8.1,但WinRT和UWP中的过程和步骤是相同的 .

相关问题