介紹
重新想象 Windows 8 Store Apps 之 背景工作
開發一個簡單的背景工作
樣本
1、通過“Windows 運行時組件”建立一個背景工作
BackgroundTaskLib/Demo.cs
/* * 背景工作 * * 註: * 背景工作項目的輸出類型需要設定為“Windows 運行時組件”,其會產生 .winmd 檔案,winmd - Windows Metadata */ using System;using System.Threading.Tasks;using Windows.ApplicationModel.Background;using Windows.Storage; namespace BackgroundTaskLib{ // 實現 IBackgroundTask 介面,其只有一個方法,即 Run() public sealed class Demo : IBackgroundTask { public async void Run(IBackgroundTaskInstance taskInstance) { // 背景工作在執行中被終止執行時所觸發的事件 taskInstance.Canceled += taskInstance_Canceled; // 非同步作業,即通知系統背景工作可在 IBackgroundTask.Run 方法返回後繼續工作 BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); try { // 指定背景工作的進度 taskInstance.Progress = 0; // taskInstance.InstanceId - 背景工作執行個體的唯一標識,由系統產生,與前台的 IBackgroundTaskRegistration.TaskId 一致 // 寫入相關資料到檔案 StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"webabcdBackgroundTask\demo.txt", CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(file, "progress: 0, currentTime: " + DateTime.Now.ToString()); for (uint progress = 10; progress <= 100; progress += 10) { await Task.Delay(1000); // 更新背景工作的進度 taskInstance.Progress = progress; // 寫入相關資料到檔案 file = await ApplicationData.Current.LocalFolder.GetFileAsync(@"webabcdBackgroundTask\demo.txt"); await FileIO.AppendTextAsync(file, "progress: " + progress.ToString() + ", currentTime: " + DateTime.Now.ToString()); } } finally { // 完成非同步作業 deferral.Complete(); } } void taskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { /* * BackgroundTaskCancellationReason - 背景工作在執行中被終止執行的原因 * Abort - 前台 app 調用了 IBackgroundTaskRegistration.Unregister(true) * Terminating - 因為系統策略,而被終止 * LoggingOff - 因為使用者登出系統而被取消 * ServicingUpdate - 因為 app 更新而被取消 */ } }}
2、示範背景工作的應用
BackgroundTask/Demo.xaml
<Page x:Class="XamlDemo.BackgroundTask.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.BackgroundTask" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnRegister" Content="註冊一個背景工作" Margin="0 10 0 0" Click="btnRegister_Click_1" /> <Button x:Name="btnUnregister" Content="取消註冊的背景工作" Margin="0 10 0 0" Click="btnUnregister_Click_1" /> </StackPanel> </Grid></Page>
BackgroundTask/Demo.xaml.cs
/* * 示範背景工作的應用 * * 註: * 1、需要引用背景工作項目,背景工作的範例程式碼在 BackgroundTaskLib/Demo.cs * 2、需要在 Package.appxmanifest 添加“背景工作”聲明,並指定 EntryPoint(背景工作的類全名) * * 另: * 關於背景工作的介紹文檔請參見本目錄下的 Introduction_to_Background_Tasks.docx 檔案 */ using System;using System.Collections.Generic;using Windows.ApplicationModel.Background;using Windows.UI.Core;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation; namespace XamlDemo.BackgroundTask{ public sealed partial class Demo : Page { // 所註冊的背景工作的名稱 private string _taskName = "demo"; // 所註冊的背景工作的 EntryPoint,即背景工作的類全名 // 需要在 Package.appxmanifest 添加“背景工作”聲明,並指定 EntryPoint 如下:<Extension Category="windows.backgroundTasks" EntryPoint="BackgroundTaskLib.Demo" /> private string _taskEntryPoint = "BackgroundTaskLib.Demo"; // 背景工作是否已在系統中註冊 private bool _taskRegistered = false; // 背景工作執行狀況的進度說明 private string _taskProgress = ""; public Demo() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 遍曆所有登入的背景工作 foreach (KeyValuePair<Guid, IBackgroundTaskRegistration> task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == _taskName) // 註:背景工作的唯一標識是 IBackgroundTaskRegistration.TaskId,此處用 Name 只是為了示範方便 { // 如果找到了指定的背景工作,則為其增加 Progress 和 Completed 事件監聽,以便前台 app 接收背景工作的進度彙報和完成彙報 AttachProgressAndCompletedHandlers(task.Value); _taskRegistered = true; break; } } UpdateUI(); } private void btnRegister_Click_1(object sender, RoutedEventArgs e) { // 用於構造一個背景工作 BackgroundTaskBuilder builder = new BackgroundTaskBuilder(); builder.Name = _taskName; // 背景工作的名稱,顯示用 builder.TaskEntryPoint = _taskEntryPoint; // 背景工作進入點,即背景工作的類全名 /* * 背景工作觸發器 IBackgroundTrigger * TimeTrigger - 需要 app 在鎖屏上,最小周期 15 分鐘 * MaintenanceTrigger - 與 TimeTrigger 類似,但是不要求 app 在鎖屏上,最小周期 15 分鐘,如果 app 不在鎖屏上則最快 2 小時執行一次 * SystemTrigger - 一般不要求 app 在鎖屏上 * SmsReceived - 接收到新的 sms 訊息時 * LockScreenApplicationAdded - app 添加到鎖屏時 * LockScreenApplicationRemoved - app 從鎖屏移除時 * OnlineIdConnectedStateChange - 當前串連的 Microsoft 帳戶更改時 * TimeZoneChange - 時區發生更改時 * ServicingComplete - 系統完成了 app 的更新時 * ControlChannelReset - 重設控制通道時,需要 app 在鎖屏上 * NetworkStateChange - 網路狀態發生改變時 * InternetAvailable - Internet 變為可用時 * SessionConnected - 工作階段狀態串連時,需要 app 在鎖屏上 * 這裡的 Session 指的是,使用者與本機之間的 Session,也就是說當切換使用者時 Session 會發生改變 * UserPresent - 使用者變為活動狀態時,需要 app 在鎖屏上 * UserAway - 使用者變為非使用中時,需要 app 在鎖屏上 * PushNotificationTrigger - 需要 app 在鎖屏上。關於“推播通知”請參見:BackgroundTask/PushNotification.xaml * ControlChannelTrigger - 需要 app 在鎖屏上。關於“推送通道”請參見:BackgroundTask/ControlChannel.xaml */ builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false)); /* * 背景工作執行條件 SystemConditionType,當背景工作觸發器觸發後,只有滿足了指定的條件才能執行 * UserPresent - 使用者為活動狀態 * UserNotPresent - 使用者為非使用中 * InternetAvailable - Internet 狀態為可用 * InternetNotAvailable - Internet 狀態為不可用 * SessionConnected - 工作階段狀態是串連的。這裡的 Session 指的是,使用者與本機之間的 Session,也就是說如果系統中有使用者登入則 SessionConnected * SessionDisconnected - 工作階段狀態是斷開的。這裡的 Session 指的是,使用者與本機之間的 Session,也就是說如果系統中沒有使用者登入(所有使用者都登出了)則 SessionDisconnected */ builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable)); // 向系統註冊此背景工作 BackgroundTaskRegistration task = builder.Register(); // task.TaskId; 擷取此背景工作的標識,一個 GUID // 為此背景工作增加 Progress 和 Completed 事件監聽,以便前台 app 接收背景工作的進度彙報和完成彙報 AttachProgressAndCompletedHandlers(task); _taskRegistered = true; UpdateUI(); } private void btnUnregister_Click_1(object sender, RoutedEventArgs e) { // 遍曆所有登入的背景工作 foreach (KeyValuePair<Guid, IBackgroundTaskRegistration> task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == _taskName) // 註:背景工作的唯一標識是 IBackgroundTaskRegistration.TaskId,此處用 Name 只是為了示範方便 { // 從系統中登出指定的背景工作。唯一一個參數代表如果當前背景工作正在運行中,是否需要將其取消 task.Value.Unregister(true); break; } } _taskRegistered = false; UpdateUI(); } private void AttachProgressAndCompletedHandlers(IBackgroundTaskRegistration task) { // 為任務增加 Progress 和 Completed 事件監聽,以便前台 app 接收背景工作的進度彙報和完成彙報 task.Progress += new BackgroundTaskProgressEventHandler(OnProgress); task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted); } private void OnProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs args) { // 擷取背景工作的執行進度 _taskProgress = args.Progress.ToString(); UpdateUI(); } private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args) { // 背景工作已經執行完成 _taskProgress = "完成"; // 如果此次背景工作的執行出現了錯誤,則調用 CheckResult() 後會拋出異常 try { args.CheckResult(); } catch (Exception ex) { _taskProgress = ex.ToString(); } UpdateUI(); } private async void UpdateUI() { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { btnRegister.IsEnabled = !_taskRegistered; btnUnregister.IsEnabled = _taskRegistered; if (_taskProgress != "") lblMsg.Text = "進度:" + _taskProgress; }); } }}
OK
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar
查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/