Lifecycle Events中的BackgroundTask可以工作,但不能自动运行

时间:2015-03-06 11:26:31

标签: windows-phone-8 windows-phone-8.1

我正在尝试使用后台任务更新辅助磁贴中的数据。当我从生命周期事件下拉列表中选择后台任务时,它会更新。但它不会自动运行。这是否意味着我的SystemTrigger没有被解雇?请帮忙。

这是我的代码:

public static void CheckIfBackgroundTaskExist()
{
   if (BackgroundTaskRegistration.AllTasks.Count < 1)
        {
            RegisterBackgroundTask();
        }     
}


public static void RegisterBackgroundTask()
{
        BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
        builder.Name = "SecondaryTileUpdate";
        builder.TaskEntryPoint = "BackgroundTaskLiveTile.SecondaryTileUpdater";

        //IBackgroundTrigger trigger = new MaintenanceTrigger(15, false);
        IBackgroundTrigger trigger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false);
        builder.SetTrigger(trigger);

        IBackgroundTaskRegistration task = builder.Register();
}

public async void Run(IBackgroundTaskInstance taskInstance)
{
        //HERE: request a deferral
        var deferral = taskInstance.GetDeferral();

        var list = await SecondaryTile.FindAllAsync();
        foreach (SecondaryTile liveTile in list)
        {
            HttpClient client = new HttpClient();
            var response = await client.GetAsync(url, HttpCompletionOption.ResponseContentRead);

            if (!response.IsSuccessStatusCode)
            {
                //
            }
            if (response != null)
            {
                string content = await response.Content.ReadAsStringAsync();
                try
                {
                    MyProperty= JsonConvert.DeserializeObject<MyClass>(content);
                }
                catch
                {
                    //MessageBox.Show("Unable to retrieve data");
                }
            }
            // Update Secondary Tiles
            if (liveTile.TileId == "MySecondaryTileId")
            {
                await UpdateMyTile();
            }
      }
}

0 个答案:

没有答案