一、介绍

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:后台任务的更多相关文章

  1. WP8.1开发:后台任务详解(求推荐)

    小梦今天给大家分享一下windows phone 8.1中的后台任务如何实现,许多应用都会用到后台任务,所以我们必须得掌握. 新建后台任务类: 首先我们先新建一个windows phone 8.1空白 ...

  2. WP8.1和Win8.1的不同之处

    本文仅是个人见解,如有不足或错误之处欢迎批评指正~ 1.Toast: 创建Toast代码差不多但实现机制及管理上不一样 2.ApplicationData: WP8.1多了一个LocalCacheFo ...

  3. 【WP8.1开发】认识后台任务

    在手机上,使用后台,不像电脑上那么随意,准确地讲嘛,在移动平台上,后台任务都有严格的限制.至于说为什么会有这么多限制,我估计初衷很明显——保证系统的性能不受某个或某几个应用的负面影响:另外就是出于安全 ...

  4. [深入浅出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# ...

  5. 【Win10 UWP】后台任务与动态磁贴

    动态磁贴(Live Tile)是WP系统的大亮点之一,一直以来受到广大用户的喜爱.这一讲主要研究如何在UWP应用里通过后台任务添加和使用动态磁贴功能. 从WP7到Win8,再到Win10 UWP,磁贴 ...

  6. 将Win8.1/WP8.1应用迁移到Universal Windows Platform

    在上一篇在VS2015 RC打开CTP中创建的工程,我们介绍了怎么在RC中打开CTP中创建的Universal 工程,这一篇我们来讲下怎么将Windows 8.1/WP8.1的应用迁移到Univers ...

  7. 【Win 10 应用开发】在App所在的进程中执行后台任务

    在以往版本中,后台任务都是以独立的专用进程来运行,因此,定义后台任务代码的类型都要位于 Windows 运行时组件项目中. 不过,在14393中,SDK 作了相应的扩展,不仅支持以前的独立进程中运行后 ...

  8. 【WP8.1】WebView笔记

    之前在WP8的时候做过WebBrowser相关的笔记,在WP8.1的WebView和WebBrowser有些不一样,在这里做一些笔记 下面分为几个部分 1.禁止缩放 2.JS通知后台C#代码(noti ...

  9. 【WP8.1】类似“IT之家” 自定义消息 的实现

    曾经在WP7.WP8下的消息 使用的都是Coding4Fun.Phone.Toolkit里面的ToastPrompt类来实现的. 现在我们来自己做个类似IT之家的这种效果:从右边弹出,经过几秒后会自动 ...

随机推荐

  1. 解决redmine写操作很慢的问题

    以前刚开始时用redmine是直接使用它的webrick服务器来运行的,后来为了提高性能,采用nginx+passenger的方式来驱动redmine,访问速度快了不少,但是在新建问题或更新问题时变得 ...

  2. spring框架IoC

    IoC反转控制, 举个反例: //数据操作类 public class DataBase { //向数据库中存储数据 public void saveDB() { } } //业务逻辑类 public ...

  3. Dynamics AX 2012 R2 安装额外的AOS

    众所周知,AX系统分为三层:Client,Application Server,Database Server. 我们添加额外的Application Server主要是出于以下两个原因: 使用多台服 ...

  4. Unix/Linux编程实践教程(0:文件、终端、信号)

    本来只打算读这本书socket等相关内容,但书写得实在好,还是决定把其余的内容都读一下. 阅读联机帮助的一个示例: open系统调用: read系统调用: Unix的time: 上面的printf可以 ...

  5. Java 利用初学知识 写出自己的名字

  6. PetaPoco的几个特性

    在PetaPoco中,Brad并没有定义太多Attribute来修饰Models或Fields.这些为数不多的几个Attribute如下: ColumnAttribute ExplicitColumn ...

  7. HBase Mac OSX 安装笔记

    本次测试安装的机器为Mac Book Pro, 系统为 OS X 10.9.4.hbase版本0.98.6.1. 使用Java版本为Oracle的JDK 1.6.0_65. 1. 下载安装 hbase ...

  8. easyui datagrid 仿ext—右键

    var createGridHeaderContextMenu = function(e, field) { e.preventDefault(); var grid = $(this);/* gri ...

  9. JAVA线程锁lock下Condition高级使用-多个Condition的整合使用

    import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.uti ...

  10. linux内核3.4基于wakeup_source的autosleep机制分析

    点击打开链接 一:wakeup_source简介: linux 3.4内核PM使用了wakeup_source来保持唤醒状态,也就是keep awake.之前android一直是基于Linux加入了w ...