代码之家  ›  专栏  ›  技术社区  ›  tavier

后台任务未在UWP中运行

  •  0
  • tavier  · 技术社区  · 9 年前

    我有一个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下拉列表挂起我的应用时,它从不执行 Run() 方法(IBackgroundTask接口的实现)。 BackgroundTask类中的代码:

    namespace NamespaceOfMyBackgroundTaskInterfaceImplementation
    {
        public sealed class BackgroundTask : IBackgroundTask
        {
            public async void Run(IBackgroundTaskInstance taskInstance)
            {
                //Code to run in the background
            }
        }
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Community CDub    8 年前

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

    关注者请查看 at this answer ,描述了步骤。它符合 银光8.1 ,但过程和步骤在 商在最新的 超宽带 .

    推荐文章