Windows Phone 8.1 应用可以添加多个后台任务,以辅助应用完成某些任务。

(1)新建前台应用

后台任务是依托于前台应用的,所以必须拥有一个前台应用。

该前台应用的功能很简单,就是读取文件中保存的文本;而后台任务就是将当前时间写入文件中。

前台界面:

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions> <Viewbox Margin="20,0">
<TextBlock x:Name="timeTextBlock"
Text="Time"/>
</Viewbox> <Grid Grid.Row="1" Margin="20,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> <Button x:Name="registerButton"
Content="Register Task"
Margin="0,0,5,0"
HorizontalAlignment="Stretch"
Click="registerButton_Click"/>
<Button x:Name="unregisterButton" Grid.Column="1"
Content="Unregister Task"
Margin="5,0,0,0"
HorizontalAlignment="Stretch"
Click="unregisterButton_Click"/>
</Grid>
</Grid>

打开应用时就读取文本信息:

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
await ShowFileText();
} private async Task ShowFileText()
{
file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Time.txt", CreationCollisionOption.OpenIfExists);
timeTextBlock.Text = await FileIO.ReadTextAsync(file);
}

(2)添加一个 Windows Runtime Component 项目

后台任务必须为 Windows Runtime Component。

(3)编写一个继承自 IBackgroundTask 接口的类

在后台任务的项目中新建一个类,并继承 IBackgroundTask 接口,实现 Run 方法,该类还必须为 sealed:

public sealed class WritingTask: IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral(); await WriteTimeToFile("Time.txt"); deferral.Complete();
}   private async Task WriteTimeToFile(string path)
  {
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, DateTimeOffset.Now.ToString());
}
}

Run 方法就是后台任务执行时的方法。

(4)前台应用 Manifest 中添加后台任务

可以设置触发器的类型,记得设置后台任务的入口点。

然后前台应用添加后台任务项目的引用。

(5)前台应用对后台任务进行注册与解除注册

最后的一步也就是在前台应用中对后台任务进行注册了:

private async void registerButton_Click(object sender, RoutedEventArgs e)
{
BackgroundExecutionManager.RemoveAccess();
await BackgroundExecutionManager.RequestAccessAsync(); RegisterTask();
} private static void RegisterTask()
{
SystemTrigger triger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false); BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = "WritingTask";
taskBuilder.SetTrigger(triger);
taskBuilder.TaskEntryPoint = typeof(ZMyBackgroundTasks.WritingTask).FullName;
taskBuilder.Register();
}

这里选择的触发器为“当时区改变时”,这只是为了方便测试,你可以根据需要自行选择。

解除注册的方法为:

private void unregisterButton_Click(object sender, RoutedEventArgs e)
{
var task = BackgroundTaskRegistration.AllTasks.Values.First();
task.Unregister(true);
BackgroundExecutionManager.RemoveAccess();
}

Windows Phone 8.1 后台任务的更多相关文章

  1. Windows 8.1 应用开发后台任务概述(Windows XAML)

    说到后台任务,这是在和许多 Android 开发者聊天的时候,经常被提起的话题之一, Windows 移动平台的后台任务的形式有别与 Android 的后台 service,简单的说在 Windows ...

  2. windows phone 8.1 开发:后台任务详解

    原文出自:http://www.bcmeng.com/backtask/ 小梦今天给大家分享一下windows phone 8.1中的后台任务如何实现,许多应用都会用到后台任务,所以我们必须得掌握. ...

  3. WPF 后台任务 等待动画 样例 && C# BackgroundWorker 详解

    运行效果: 前台代码: <Window x :Class="Waiting.Window1" xmlns="http://schemas.microsoft.com ...

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

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

  5. 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务

    [源码下载] 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后 ...

  6. 重新想象 Windows 8 Store Apps (65) - 后台任务: 音乐的后台播放和控制

    [源码下载] 重新想象 Windows 8 Store Apps (65) - 后台任务: 音乐的后台播放和控制 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台 ...

  7. 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传

    [源码下载] 重新想象 Windows 8 Store Apps (66) - 后台任务: 下载和上传 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 后台 ...

  8. 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知

    [源码下载] 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 推送通 ...

  9. 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel)

    [源码下载] 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel) 作者:webabcd 介绍重新想象 Windows 8 Store ...

随机推荐

  1. 推广一下新Blog www.hrwhisper.me

    新博客地址:www.hrwhisper.me 欢迎互访加友链~

  2. 【软件project】机房收费系统之图形回想

    [背景]通过一阶段的学习.自己整理了整理机房收费系统.以下想通过几张图来回顾一下机房的总体流程.此图形仅仅代表鄙人现阶段的理解.本文仅供參考,若有不妥的地方,请积极指正. 1.机房收费系统业务流程图 ...

  3. C# 反射具体解释

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  4. iOS Threading编程指南 官方文档翻译第一篇(序言)

    序言   Thread是能够使多个code paths 在同一个APP内并发运行的几种技术之一.虽然新的技术为并发运行提供了先进.高效的工具(例如operation 对象和GCD),但是OS X和iO ...

  5. html5 audio标签相关知识点总结

    1.audio指JS原生对象,假如用jquery获取到audio标签后,需要dom[0]转为原生JS对象 if(audio.paused){ //如果音频暂停,就播放 audio.play(); }e ...

  6. Web自动化测试 Selenium+Eclipse+Junit+TestNG+Python

    Selenium+Eclipse+Junit+TestNG+Python 第三步 下载Selenium IDE.SeleniumRC.IEDriverServer.SeleniumClient Dri ...

  7. springboot 使用FreeMarker模板(转)

    在spring boot中使用FreeMarker模板非常简单方便,只需要简单几步就行: 1.引入依赖: <dependency> <groupId>org.springfra ...

  8. bootstrap课程4 bootstrap的css样式有哪些内容需要注意

    bootstrap课程4 bootstrap的css样式有哪些内容需要注意 一.总结 一句话总结: 1.如何选择产品(框架)的版本? 大版本下的最后一个版本,但是同时又要选择稳定的版本,也就是如果做产 ...

  9. Linux平台Makefile文件的编写基础篇

    目的:        基本掌握了 make 的用法,能在Linux系统上编程. 环境:        Linux系统,或者有一台Linux服务器,通过终端连接.一句话:有Linux编译环境. 准备: ...

  10. angular 引入material-ui

    第一步:安装material和cdk和animations,一个也不能缺,否则会报错. npm install --save @angular/material @angular/cdk @angul ...