wp8.1 Study15:后台任务
一、介绍
1、多任务处理
什么是多任务处理?它意味着当App被挂起时,它仍然可以完成一些开发者设定的任务,比如更新tiles和toasts、预定toast和提醒、后台任务等。
2、后台任务
App可以注册后台任务,它被系统运行和管理,但它依旧使用和前台程序一样的数据储存等,同时它使用的CPU资源是由系统限制的。它可以使用toast, tile, badge UI等。一个App可以注册使用多个后台任务。
二、添加后台任务
1、添加一个新的项目到解决方案中作为后台任务,如图

然后,添加以下类似代码(重点是添加IBackgroundTask)
public sealed class TheTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
taskInstance.canceled+=(s,e)=>{};//当后台任务被限制执行时触发事件
taskInstance.Progress = ;//后台任务的进度
deferral.Complete();
}
2、后台任务触发设置(在前台程序,不是在TheTask)
后台任务将根据触发器是否被触发而运行,以下是多种触发器:


同时也可以添加条件,让后台任务根据条件运行,可以设置一下几种条件等

添加条件代码很简单,代码如下:

3、在manifest声明 中,添加后台任务声明

4、请求及注册后台任务(在前台程序中)
async void RegisterBackgroundTasks()
{
// On Windows, RequestAccessAsync presents the user with a confirmation
// dialog that requests that an app be allowed on the lock screen.
// On Windows Phone, RequestAccessAsync does not show any user confirmation UI
// but *must* be called before registering any tasks
var access = await BackgroundExecutionManager.RequestAccessAsync(); // A 'good' status return on Phone is BackgroundAccess.AllowedMayUseActiveRealTimeConnectivity
if (access == BackgroundAccessStatus.Denied)
{
// Either the user has explicitly denied background execution for this app
// or the maximum number of background apps across the system has been reached
// Display some informative message to the user...
}
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = "MyBackgroundTask"; // Many different trigger types could be used here
SystemTrigger trigger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false);
taskBuilder.SetTrigger(trigger);
taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable)); // Entry point is the full name of our IBackgroundTask implementation
// Good practice to use reflection as here to ensure correct name
taskBuilder.TaskEntryPoint = typeof(MyBackgroundTask.TheTask).FullName; BackgroundTaskRegistration registration = taskBuilder.Register(); // Optionally, handle the progress/completed events of the task
registration.Progress += registration_Progress;
registration.Completed += registration_Completed; }
5、获取后台任务及取消注册后台任务,代码如下:
// AllTasks is a dictionary <Guid, IBackgroundTaskRegistration> so you can get back
// to your registration by id or by posiiton, or select First if you only have one registration.
var taskRegistration = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(); // We could then unregister the task, optionally cancelling any running instance
if (taskRegistration != null)
{
taskRegistration.Unregister(true);
} // Release the progress/completed event subscriptions
registration.Progress -= registration_Progress;
registration.Completed -= registration_Completed;
tips:在Debugger 中我们可以这样触发后台任务,如图

wp8.1 Study15:后台任务的更多相关文章
- WP8.1开发:后台任务详解(求推荐)
小梦今天给大家分享一下windows phone 8.1中的后台任务如何实现,许多应用都会用到后台任务,所以我们必须得掌握. 新建后台任务类: 首先我们先新建一个windows phone 8.1空白 ...
- WP8.1和Win8.1的不同之处
本文仅是个人见解,如有不足或错误之处欢迎批评指正~ 1.Toast: 创建Toast代码差不多但实现机制及管理上不一样 2.ApplicationData: WP8.1多了一个LocalCacheFo ...
- 【WP8.1开发】认识后台任务
在手机上,使用后台,不像电脑上那么随意,准确地讲嘛,在移动平台上,后台任务都有严格的限制.至于说为什么会有这么多限制,我估计初衷很明显——保证系统的性能不受某个或某几个应用的负面影响:另外就是出于安全 ...
- [深入浅出WP8.1(Runtime)]Windows Phone 8.1和Silverlight 8.1的区别
1.2.2 Windows Phone 8.1应用程序模型 Windows Phone 8.1支持多种开发语言来开发应用程序,包括C#.VB.JavaScript和C++,那么本书的代码主要是采用C# ...
- 【Win10 UWP】后台任务与动态磁贴
动态磁贴(Live Tile)是WP系统的大亮点之一,一直以来受到广大用户的喜爱.这一讲主要研究如何在UWP应用里通过后台任务添加和使用动态磁贴功能. 从WP7到Win8,再到Win10 UWP,磁贴 ...
- 将Win8.1/WP8.1应用迁移到Universal Windows Platform
在上一篇在VS2015 RC打开CTP中创建的工程,我们介绍了怎么在RC中打开CTP中创建的Universal 工程,这一篇我们来讲下怎么将Windows 8.1/WP8.1的应用迁移到Univers ...
- 【Win 10 应用开发】在App所在的进程中执行后台任务
在以往版本中,后台任务都是以独立的专用进程来运行,因此,定义后台任务代码的类型都要位于 Windows 运行时组件项目中. 不过,在14393中,SDK 作了相应的扩展,不仅支持以前的独立进程中运行后 ...
- 【WP8.1】WebView笔记
之前在WP8的时候做过WebBrowser相关的笔记,在WP8.1的WebView和WebBrowser有些不一样,在这里做一些笔记 下面分为几个部分 1.禁止缩放 2.JS通知后台C#代码(noti ...
- 【WP8.1】类似“IT之家” 自定义消息 的实现
曾经在WP7.WP8下的消息 使用的都是Coding4Fun.Phone.Toolkit里面的ToastPrompt类来实现的. 现在我们来自己做个类似IT之家的这种效果:从右边弹出,经过几秒后会自动 ...
随机推荐
- 关于SSIS中解密数据库字符串的方法
此文章适合于SSIS新手,我是个小白,在繁复查阅资料后仍无果到最后解决问题,走了很多弯路,现在讲其中一些关于SSIS的理解写出来,供大家参考,在正文之前,我就我自己的理解,阐明一些概念. 什么是SSI ...
- 创建空列表遇到的问题-RF
正确的方法:使用Create List,后面为空即可
- CAN总线抓包
马六: 由此可见, 马6的7e9跟7e8反馈的数据差不太多. 而标志508反馈的情况则不同, 波箱跟发动机反馈完全不同: 宝马3的数据如下: 证明宝马也有7e8跟7ec, 但是貌似7e8是主流啊... ...
- 用hashMAP或ArrayList解决recylerView中checkbox的选择错乱问题。
//这个监听一定要放在checkbox初始化的方法之前,否则无效.是因为滑动的时侯会重新给checkbox赋值造成的.holder.cbFileSel.setOnCheckedChangeListen ...
- androidBroadCast总结
BoradCast广播1.接受广播 BroadCastReceiver(接收系统的广播) 1-1:电话的广播 1-1-1:拨打电话的广播 1.创建一个类,继承BoradcastReceiver 2.重 ...
- python学习之if语句
1.if条件表达式判断 ##判断条件是true or false var1=10 if var1: print("true") print(var1) else: print(&q ...
- c++实现蛇形矩阵总结
蛇形矩阵,百度了一下,是这么一个东西: 像一条蛇一样依次递增. 我想,竟然做了螺旋矩阵,那做一下这个吧.在之前的螺旋矩阵的main函数基础上,写个函数接口就行了,这一次做的很快,但是这个矩阵感觉比螺旋 ...
- iOS Error
1),'libxml/tree.h' file not found Solution: 1. 导入libxml2.dylib 包 2.设置Header Search Paths 为 /usr/inc ...
- php多维数组去除空元素
在php中去除数组中的空值可以使用array_filter() 这个函数 但是这个函数只能对一维数组起作用,一旦需要对多维数组去空就不行了,而且去除的空也包括(int)0,(string)0,使用起来 ...
- Apache Solr 访问权限控制
Current state of affairs SSL support was added in version 4.2 (SolrCloud v4.7). Protection of Zookee ...