/// <summary> /// Work that should be performed from the background agent. /// </summary> /// <returns>Awaitable task is returned.</returns> public async Task TimedBackgroundWorkAsync(CancellationToken ct) { try { // Perform work that needs to be done on a background task/agent... if (this.AuthManager?.IsAuthenticated() != true) { return; } this.Notifications.DisplayToast(this.ViewModel); // SAMPLE - Load data from your API, do any background work here. using (var api = new ClientApi()) { var data = await api.GetItems(ct); if (data != null) { var items = data.ToObservableCollection(); if (items.Count > 0) { var index = DateTime.Now.Second % items.Count; this.Notifications.DisplayToast(items[index]); } } ct.ThrowIfCancellationRequested(); if (BackgroundWorkCost.CurrentBackgroundWorkCost <= BackgroundWorkCostValue.Medium) { // Update primary tile await this.Notifications.CreateOrUpdateTileAsync(new ModelList <ItemModel>(data)); ct.ThrowIfCancellationRequested(); // Update all tiles pinned from this application await this.Notifications.UpdateAllSecondaryTilesAsync(ct); } } } catch (OperationCanceledException) { throw; } catch (Exception ex) { this.Logger.LogErrorFatal(ex, "Failed to complete BackgroundWork from background task due to: {0}", ex.Message); throw ex; } }
protected override async Task OnRefreshAsync(bool forceRefresh, CancellationToken ct) { if (forceRefresh || this.Items == null || this.HasLocationChanged) { this.ShowBusyStatus(Resources.TextLoading, this.Items == null || this.Items.Count == 0); using (var api = new ClientApi()) { this.Items.UpdateRange(await api.GetItems(ct), true); this.LastUpdated = DateTime.Now; } // Save to cache await this.SaveToCacheAsync(() => this.Items); await this.SaveToCacheAsync(() => this.LastUpdated); } }