Things to note when creating background task for tile update:

  1. Background task class has to be in separate “Windows Runtime Component” project.
    image
  2. Don’t forget to add background task project as a reference to application.
    image
  3. Update “Package.appxmanifest”:
    1. In “Declarations” tab create “Background Task” declaration, check “Timer” and set “Entry Point” to background tasks class (Namespace.ClassName).
      image
    2. In “Visual Assets” provide default tile image, in my case I only update square tile.
      image
  4. Create “OnNavigatedTo” event in your application startup class and register background task class, minimal update interval is 15min:
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            RegisterBackgroundTask();
        }

        //Custom name to of background task
        private const string TaskName = "MyCustomTileUpdater";
        //                                     Namespace     .ClassName
        private const string TaskEntryPoint = "BackgroundTask.AppTileUpdater";

        private async void RegisterBackgroundTask()
        {
            var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
            //Check for permissions to use bakground tasks
            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;
                //Update interval
                taskBuilder.SetTrigger(new TimeTrigger(15, false));
                var registration = taskBuilder.Register();
            }
        }

 

Full working example with custom tile template and background task class can be found in GitHub.

Example of custom tile.

image

Comments


Comments are closed