一、前言
昨天更新了10天前上架到windows8.1平台和windowsphone平台的通用应用“51单片机汇编”,总要是添加了动态磁贴以及ListView的Groupstyle应用。下面主要主要复习下如何利用后台任务添加动态磁贴
二、动态磁贴
动态磁贴也是前几天才掌握的,基本内容可以查看我之前的有关动态磁贴博文。在这里我理顺在我的应用中,利用backgroudtask动态更新磁贴。
step1:首先先添加backgroutask组件。如图一、二
step2:
将backgroundtask引用到HubApp.windows及HubApp.windowsphone目录下,如下图:
step3:改写backgrountask下的HubAppBackgroundTask类(具体名字可以任改),这是重要的,因为后台实际执行的内容就是在这个类下编写如图
添加接口IBackgroundTask,实现接口代码如下:
public sealed class HubAppBackgroundTask:IBackgroundTask { private static string FILENAME = "CodeCopyFile.XML"; public async void Run(IBackgroundTaskInstance taskInstance) { BackgroundTaskDeferral deferral = taskInstance.GetDeferral();//必要的 try//具体后台执行代码 { var code=await GetRandomCode();//磁贴的数据 if (code!=null) { TileSetter.CreatTiles(code);//更新磁贴 } } catch (Exception) { throw; } finally { deferral.Complete();//必要的 } } }
public static void CreatTiles(CollectCode code) { string TileSquare150x150Image = @"ms-appx:///Assets/SmallLogo.scale-240.png"; string TileSquare310x150Image = @"ms-appx:///Assets/WideLogo.scale-240.png"; XmlDocument tileXML = new XmlDocument(); //////////////////////////////////////////////////////// // Find all the available tile template formats at: // http://msdn.microsoft.com/en-us/library/windows/apps/Hh761491.aspx string tileString = "<tile>" + "<visual version=\"2\">" + "<binding template=\"TileSquare150x150PeekImageAndText03\" fallback=\"TileSquarePeekImageAndText03\">" + "<image id=\"1\" src=\"" + TileSquare150x150Image + "\" alt=\"alt text\"/>" + "<text id=\"1\">" + code.Title + "</text>" + "<text id=\"2\">" + code.Subtitle + "</text>" + "</binding>" + "<binding template=\"TileWide310x150PeekImage01\" fallback=\"TileWidePeekImage01\">" + "<image id=\"1\" src=\"" + TileSquare310x150Image + "\" alt=\"alt text\"/>" + "<text id=\"1\">" + code.Title + "</text>" + "<text id=\"2\">" + code.Subtitle + "</text>" + "</binding>" + "</visual>" + "</tile>"; tileXML.LoadXml(tileString); //新建磁贴通知 TileNotification tile = new TileNotification(tileXML); //更新磁贴通知 TileUpdater updateTiler = TileUpdateManager.CreateTileUpdaterForApplication(); updateTiler.EnableNotificationQueue(false); updateTiler.Clear(); updateTiler.Update(tile); } } ... public sealed class CollectCode { public CollectCode(String uniqueId, String title, String subtitle, int count) { this.UniqueId = uniqueId; this.Title = title; this.Subtitle = subtitle; this.Count = count; } public string UniqueId { get; set; } public string Title { get; set; } public string Subtitle { get; set; } public int Count { get; set; } }
至此backgroundtask已经全部准备好。
step3:在应用中注册相应的backgroundtask
首先要在Package.appxmanifest清单上声明后台任务,如下图:
然后再在应用上代码注册后台任务。在51单片机汇编上,我在App.xaml.cs注册后台任务,C#代码如下:
protected async override void OnLaunched(LaunchActivatedEventArgs e) { ... // 确保当前窗口处于活动状态 Window.Current.Activate(); Init(); } private async void Init() { await BackgroundTaskHelper.Register(); } .... public sealed class BackgroundTaskHelper { static string taskName = "BackgroundTask"; static string taskEntryPoint = "BackgroundTask.HubAppBackgroundTask"; public static async Task<bool> Register(Action action = null) { try { UnRegister(); // do the registeration // check access permission BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync(); switch (status) { case BackgroundAccessStatus.Denied: // reach maxmium number, or, disabled by user return false; case BackgroundAccessStatus.Unspecified: return false; case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity: break; } // register the task in the next step. BackgroundTaskBuilder builder = new BackgroundTaskBuilder(); builder.Name = taskName; builder.TaskEntryPoint = taskEntryPoint; builder.SetTrigger(new TimeTrigger(15, false)); // run every 15 minutes var registration = builder.Register(); if (registration != null && action != null) { registration.Completed += (s, a) => { action(); }; } return true; } catch { return false; } } public static bool UnRegister() { try { BackgroundTaskRegistration task = null; // Check for existing registrations of this background task. foreach (var cur in BackgroundTaskRegistration.AllTasks) { if (cur.Value.Name == taskName) { // The task is already registered. task = (BackgroundTaskRegistration)(cur.Value); break; } } if (task != null) { task.Unregister(false); } return true; } catch { return false; } } }
(这个注册后台任务代码,是借鉴博主@MS-UAP http://www.cnblogs.com/ms-uap/ 的应用的代码的-=-)
至此应用已经可以会在后台动态更新自己的磁贴了。
三、后话
昨天更新时登录上windows及windowsphone上的开发中心看了下51单片机汇编的下载情况,还蛮不错的有1000多了(对真正第一开发应用的我来说还是很爽),如图
没想到的是windows8.1会有700多下载,看来windows8.1平板用户比windowsphone用户还多哈。
明天又要下广州回学校了=-=大三下学期加油吧!大三再在空余时间开发个什么让自己学习下呢????