Windows 8 应用开发 - 挂起与恢复
Windows 8 应用通常涉及到两种数据类型:应用数据与会话数据。在上一篇提到的本地数据存储就是应用层面的数据,包括应用参数设置、用户重要数据等。那么会话层面的数据是基于用户每次使用应用而形成,这些数据可能不需要留存在设备中。在整个应用生命周期中,应用启动后便进入运行状态。当用户离开或系统进入待机状态时,应用会进入挂起状态,此时应用将被放入到内存中,待用户重新使用时便会恢复成运行状态。

在这个过程中用户之前可能已经录入了一些数据,并且希望在应用恢复时可以继续进行录入。对于开发者来说,我们需要在应用挂起时将一些会话数据进行保存,当应用恢复后同时将暂存数据复原,以便让用户继续使用。需要注意的是MSDN中提到:“当用户通过按 Alt+F4 或使用关闭手势关闭应用时,应用将被挂起 10 秒钟然后被终止。”也就意味着关闭的应用只有10秒钟时间可以被恢复。下面将通过实例进行演示,首先创建一个Textbox 让用户录入名字进行会话操作。我们首先来尝试一下没有进行挂起暂存处理的应用是何种结果。
<StackPanel Grid.Row="1" Margin="120,30,0,0">
<StackPanel Orientation="Horizontal" Margin="0,20,0,20">
<TextBlock Text="Name: " Style="{StaticResource BasicTextStyle}" Width="50"/>
<TextBox x:Name="nameInput" Width="200"/>
</StackPanel>
</StackPanel>
挂起
直接按F5运行应用,在Name 栏中输入名字或任意字符。在VS2012的Debug Location 工具栏可以看到挂起(Suspend )的选项,我们选择挂起并终止(Suspend and shutdown),程序挂起后从系统左侧菜单栏里找到之前的应用重新启用,恢复后的应用Name 栏中的文字已经丢失。对于名字这样的简单录入还可以接受,如果录入项较多的话那将损失惨重。

接下来我们将进行应用挂起处理,打开App.xaml.cs 程序,在OnLaunched 方法中创建了rootFrame,当rootFrame 为Null 时将重新创建Frame,在这个逻辑判断中要使用SuspensionManager.RegisterFrame 方法进行rootFrame 注册,这样才可以使应用获得根Frame 信息并进行数据存储。
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame(); SuspensionDemo.Common.SuspensionManager.RegisterFrame(rootFrame, "appFrame"); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
} // Place the frame in the current Window
Window.Current.Content = rootFrame;
}
在OnSuspending 方法中,使用SuspensionManager.SaveAsync 方法将挂起应用的当前状态进行保存,这里可以调用异步操作来进行处理。
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
await SuspensionDemo.Common.SuspensionManager.SaveAsync();
deferral.Complete();
}
注册完成后,打开MainPage.xaml.cs 在SaveState 方法中添加如下代码,使应用挂起时能将Name 字段保存起来。
protected override void SaveState(Dictionary<String, Object> pageState)
{
pageState["name"] = nameInput.Text;
}
恢复
挂起操作完成后,就要进行恢复操作,将暂存的数据恢复到应用中。再次打开App.xaml.cs 在PreviousExecutionState 判断为Terminated 时加入SuspensionManager.RestoreAsync 方法恢复以前的应用状态。
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{ Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first
rootFrame = new Frame(); SuspensionDemo.Common.SuspensionManager.RegisterFrame(rootFrame, "appFrame"); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
await SuspensionDemo.Common.SuspensionManager.RestoreAsync();
} // Place the frame in the current Window
Window.Current.Content = rootFrame;
} if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
{
throw new Exception("Failed to create initial page");
}
}
// Ensure the current window is active
Window.Current.Activate();
}
最后,在MainPage.xaml.cs 的LoadState 方法中将pageState的Name 字段内容恢复即可。我们再次F5运行应用;录入姓名;挂起并终止应用,应用恢复后可以看到之前录入的姓名仍然存在。
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
if (pageState != null && pageState.ContainsKey("name"))
{
nameInput.Text = pageState["name"].ToString();
}
}
源码下载
Windows 8 应用开发 - 挂起与恢复的更多相关文章
- 【本人译作推荐】Windows 8应用开发:C#和XAML卷(原名:Building Windows 8 Apps with C# and XAML)
[图书推荐] 译名:Windows 8应用开发:C#和XAML卷 原名:Building Windows 8 Apps with C# and XAML 编辑推荐 国内第一本使用XAML与C#语言 ...
- .Net Core 3 骚操作 之 用 Windows 桌面应用开发 Asp.Net Core 网站
前言 曾经在开发 Asp.Net 网站时就在想,为什么一定要把网站挂到 IIS 上?网站项目的 Main 函数哪儿去了?后来才知道这个 Main 函数在 w3wp.exe 里,这也是 IIS 的主进程 ...
- 利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
Dixon 原文 用ArcGIS Engine.VS .NET和Windows控件开发GIS应用 此过程说明适合那些使用.NET建立和部署应用的开发者,它描述了使用ArcGIS控件建立和部署 ...
- MFC任务管理器task manager----进程的挂起与恢复--NtSuspendProcess&&NtResumeProcess
http://hi.baidu.com/xbbsh/blog/item/b73d3125462201084c088db1.html ---------------------------------- ...
- VMware Authorization Service不能启动 VMware虚拟机状态已挂起无法恢复解决方案
在网上看说在服务里面启动 但也是不能用 电脑上说是WINDOWS无法启动VMware Authorization Service服务(位于本地计算机上)错误:1068 依赖服务或组无法启动 这个很简单 ...
- windows phone 网络开发三部曲(一)各种包的各种抓法
首先感谢大家对我上一篇博客的支持,让我也体验了一把上榜的感觉. 这无疑是对我这个刚刚打算,认真写写博客的人的莫大的鼓励,再次感谢(鞠躬)!! 接下来想和大家分享一些关于windows phone网络开 ...
- Setting up a EDK II build environment on Windows and Linux:搭建Windows和Linux开发环境[2.2]
Setting up a EDK II build environment on Windows and Linux:搭建Windows和Linux开发环境[2.2] 2015-07 北京海淀区 ...
- Windows 网络通讯开发
Windows 网络通讯开发 一.Windows网络开发API 由于C++标准库中没有网络库,所以进行网络开发的时候要调用系统API.Windows通讯开发API包括以下几个基本函数及成员类型: 1. ...
- 《Windows IoT 应用开发指南》
物物互联的时代已经到来,智能家居.智慧校园.智慧交通.可穿戴.无人机.全息投影,各种各样的新名词.黑科技层出不穷.当我们为五年前能够通过手机控制家电而欣喜若狂的时候,可曾憧憬过当前使用增强现实设备完成 ...
随机推荐
- QT实现不规则窗体
看到网上有很多不规则窗体的实现,效果很酷.于是使用QT也实现了一个,QT的不规则窗体实现非常简单,只需要设置一个mask(遮掩)图片,这个图片的格式可以使用png或bmp格式,我使用了png格式,默认 ...
- Java Web Services (1) - 第1章 Web服务快速入门
SCRIPTS_DIR=/Users/liuzhaofu/opus-dev/product/tools/devPRODUCT_DIR=/Users/liuzhaofu/opus-dev/product ...
- [Android学习笔记]SeekBar的使用
一.SeekBar滑动条的使用 xml声明: <SeekBar android:id="@+id/seekbar" android:layout_width="20 ...
- Android 实现自己定义多级树控件和全选与反选的效果
博文開始之前,首先要感谢大牛:(lmj623565791),本博文是在其博文http://blog.csdn.net/lmj623565791/article/details/40212367基础上进 ...
- Codeforce 57C Array
C. Array time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- Java程序猿面试题集(181- 199)
Java面试题集(181-199) 摘要:这部分是包括了Java高级玩法的一些专题,对面试者和新入职的Java程序猿相信都会有帮助的. 181. 182. 183. 184. 185. 186. 1 ...
- ipsec vpn私网数据大量掉包问题
周四出现了一个很奇葩的问题,所有的站点的VPN通信都是正常的,唯独郑州节点和中心节点的私网数据长ping掉包量达到20%左右,在中心节点ping郑州节点公网IP没有发现掉包问题,故障排除如下: 1.测 ...
- Java程序猿学习当中各个阶段的建议
回答阿里社招面试如何准备,顺便谈谈对于Java程序猿学习当中各个阶段的建议 引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的 ...
- Cordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaScript访问原生的设备功能,如摄像头、麦克风等。
Cordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaScript访问原生的设备功能,如摄像头.麦克风等. Cordova还提供了一组统一的JavaScript类库,以及为这 ...
- SE 2014年4月22日(一)
实验 练习: 如图配置: 两自治系统 AS 100 和 AS 200 AS 100 是由两私有自治系统 (AS 65001 和 AS 65002)构成 要求配置BGP联盟 使得 R3 R4 R5 下 ...