一、介绍

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. 夺命雷公狗-----React---4--props变量的传递

    提示:props的值是不可以改变的... <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  2. Spark分析笔记

    前言 第一章 Spark简介 本章将对Spark做一个介绍,以及它的一些基本概念 Spark是什么? Spark生态系统BDAS Spark架构 Spark分布式与单机多核架构的异同 Spark的企业 ...

  3. Eclipse安装SVN插件方式简明介绍

    一.Links安装: 推荐使用此种安装方式,因为它便于插件的管理. 在eclipse根目录下新建文件夹links,这样就得到了eclipse\links 在eclipse\links下新建一个link ...

  4. 2015弱校联盟(1) -A. Easy Math

    A. Easy Math Time Limit: 2000ms Memory Limit: 65536KB Given n integers a1,a2,-,an, check if the sum ...

  5. Double Buffering Windows Forms

    Double Buffering Windows Forms As much as we would like it not to be the case, graphics can be slow ...

  6. [问题2015S04] 复旦高等代数 II(14级)每周一题(第五教学周)

    [问题2015S04] 设 \(A\) 为 \(n\) 阶方阵, \(C\) 为 \(k\times n\) 矩阵, 且对任意的 \(\lambda\in\mathbb{C}\), \(\begin{ ...

  7. Exception&Error

    Java异常处理 1:什么是异常 异常(Exception)也叫异常.在Java编程语言中,异常就是程序在运行过程中由于硬件设备问题.软件设计错误.缺陷等导致的程序错误. 1.1:想打开的文件不存在 ...

  8. EasyUI关于 numberbox,combobox,validatebox 的几个小问题

    在最近的项目中,首次使用到了 网页的一个布局框架——EasyUI,感觉这个框架特别牛,兼容性很不错,页面效果也挺不错,可是在使用标题上三个控件过程中遇到几个很奇特的问题,让我头疼不已,所以在此给广大I ...

  9. java关于时间

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...

  10. DAL、DAO、ORM、Active Record辨析

    转自:http://blog.csdn.net/suiye/article/details/7824943 模型 Model 模型是MVC中的概念,指的是读取数据和改变数据的操作(业务逻辑).一开始我 ...