using Windows.ApplicationModel.Background; using Windows.UI.Notifications; public sealed class LiveTileUpdater : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { // Create notification content var template = TileTemplateType.TileSquare150x150Text01; var content = TileContentFactory.CreateTileSquare150x150Text01(); content.TextBody1.Text = "Hello world!"; // Create notification var notification = new TileNotification(content.GetXml()); TileUpdateManager.CreateTileUpdaterForApplication().Update(notification); // Complete the task taskInstance.GetDeferral().Complete(); } }
using System; using System.Net.Http; using Windows.ApplicationModel.Background; using Windows.Storage; public sealed class ImageDownloader : IBackgroundTask { public async void Run(IBackgroundTaskInstance taskInstance) { // Download image from URL var httpClient = new HttpClient(); var response = await httpClient.GetAsync("https://example.com/image.jpg"); var imageBytes = await response.Content.ReadAsByteArrayAsync(); // Save image to local storage var localFolder = ApplicationData.Current.LocalFolder; var file = await localFolder.CreateFileAsync("image.jpg", CreationCollisionOption.ReplaceExisting); await FileIO.WriteBytesAsync(file, imageBytes); // Complete the task taskInstance.GetDeferral().Complete(); } }Package Library: Both of these examples use the Windows Runtime API, which is included in the Windows.Foundation.UniversalApiContract package. This package is automatically referenced when developing Windows Store apps in C#.