在后台任务中的一天后,Live Tiles更新开始。如何立即启动

时间:2014-01-11 08:20:49

标签: c# visual-studio-2012 rss windows-store-apps live-tile

我开发了一个Windows 8应用程序,它涉及通过后台任务调用Live tile,从而显示某些RSS源。

然而,当我安装应用程序并右键单击磁贴时,按钮应用栏没有关闭/打开实时磁贴的按钮,即动态磁贴不起作用。

然而,在一天或12小时内,Live tile开始自动更新。

如何让Live贴片在安装后立即运行?(请记住这些是要显示的Rss源)。

我的代码 -

private async void RegisterBackgroundTask()
    {
        try
        {
            var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
            if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
            backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
            {
                foreach (var task in BackgroundTaskRegistration.AllTasks)
                {
                    if (task.Value.Name == taskName)
                    {
                        task.Value.Unregister(true);
                    }
                }

                BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
                taskBuilder.Name = taskName;
                taskBuilder.TaskEntryPoint = taskEntryPoint;
                taskBuilder.SetTrigger(new TimeTrigger(15, false));
                var registration = taskBuilder.Register();
            }
        }
        catch
        { }
     }

2 个答案:

答案 0 :(得分:1)

假设你的IBackgroundTask的Run方法中的函数UpdateTile()真正进行了tile更新,那么在IBackgroundTask类中调用该UpdateTile()方法的公共方法。

public sealed class TileUpdater : IBackgroundTask
{    
    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        // Get a deferral, to prevent the task from closing prematurely 
        // while asynchronous code is still running.
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        // Update the live tile with the names.
        UpdateTile(await GetText());

        // Inform the system that the task is finished.
        deferral.Complete();
    }
    public async static void RunTileUpdater()
    {
        UpdateTile(await GetText());
    }
}

然后在

之后调用应用程序代码中的RunTileUpdater()
var registration = taskBuilder.Register();
TileUpdater.RunTileUpdater(); // <<<<-----

答案 1 :(得分:1)

您的应用必须至少启动一次才能进行后台任务注册。注册后,您的后台任务将在指定的间隔过后每次运行。