【问题标题】:Windows store app 8.1 - Background Task not triggeredWindows 商店应用程序 8.1 - 未触发后台任务
【发布时间】:2015-07-20 21:28:42
【问题描述】:

我对 Windows 应用商店应用非常陌生,我正在尝试使用后台任务。

我以这种方式在按钮单击事件中注册它: var builder = new BackgroundTaskBuilder();

         builder.Name = "bikePositionUpdate";
         builder.TaskEntryPoint = "BackgroundTaskGps.BikeGPSPositionUpdateBackgroundTask";
         builder.SetTrigger(new TimeTrigger(15, false)); // 

         BackgroundTaskRegistration taskRegistration = builder.Register();

所以它应该在15'内启动。

这是我的任务:

namespace BackgroundTaskGps
{
    public sealed class BikeGPSPositionUpdateBackgroundTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
             Debug.WriteLine("run run run");
        }
    }
} 

这里是我的清单

...
      <Extensions>
        <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundTaskGps.BikeGPSPositionUpdateBackgroundTask">
          <BackgroundTasks>
            <Task Type="timer" />
          </BackgroundTasks>
        </Extension>
      </Extensions>
...

但是 run 方法永远不会被命中。

请问我的问题是什么?

PS:我正在本地机器(同一台开发 PC)上测试我的应用程序。

请注意,我说的不是 Windows Phone 应用程序,而是 Windows 应用程序

【问题讨论】:

    标签: c# windows-store-apps background-task


    【解决方案1】:

    对于 Windows Phone 你必须调用这个

    await BackgroundExecutionManager.RequestAccessAsync();
    

    所以完整的代码是

    await BackgroundExecutionManager.RequestAccessAsync();
     builder.Name = "bikePositionUpdate";
             builder.TaskEntryPoint = "BackgroundTaskGps.BikeGPSPositionUpdateBackgroundTask";
             builder.SetTrigger(new TimeTrigger(15, false)); //For Windows Phone 8.1 minimum is 15 minutes
    
             BackgroundTaskRegistration taskRegistration = builder.Register();
    

    【讨论】:

    • WP 8.1 最短时间为 30 分钟。
    • 不,是 15 分钟。我在 15 分钟运行我的应用程序。
    • 您永远不知道后台任务何时触发。它可以是 0 到 30 分钟之间的任何时间。我看到后台任务在同一分钟结束和开始。至少在 Windows Phone 8.1 中没有办法知道它何时开始或何时停止
    • Link here 'msdn.microsoft.com/en-us/library/windows/apps/xaml/…' 它说 WP 需要 30 分钟。但是,是的,我看到事件在设置五分钟后触发。
    • 请注意我说的不是Windows Phone App,而是Windows App
    【解决方案2】:

    您正在使用 TimeTrigger 任务来响应触发器(触发)。 TimeTrigger Task 之间的最短时间必须为 30 分钟。因此,您可以每 30 分钟执行一次后台任务。它类似于 Windows 8 中的定期触发器。所以您更新的代码行将是:

    builder.SetTrigger(new TimeTrigger(30, false));
    

    【讨论】:

    • 商店应用的最短时间为 15 分钟,但电话为 30 分钟。
    • 请注意我说的不是Windows Phone App,而是Windows App
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多