设为首页 收藏本站
查看: 1219|回复: 0

Windows 8 应用开发

[复制链接]

尚未签到

发表于 2015-5-22 08:29:17 | 显示全部楼层 |阅读模式
  Windows 8 应用通常涉及到两种数据类型:应用数据与会话数据。在上一篇提到的本地数据存储就是应用层面的数据,包括应用参数设置、用户重要数据等。那么会话层面的数据是基于用户每次使用应用而形成,这些数据可能不需要留存在设备中。在整个应用生命周期中,应用启动后便进入运行状态。当用户离开或系统进入待机状态时,应用会进入挂起状态,此时应用将被放入到内存中,待用户重新使用时便会恢复成运行状态。
DSC0000.png
  在这个过程中用户之前可能已经录入了一些数据,并且希望在应用恢复时可以继续进行录入。对于开发者来说,我们需要在应用挂起时将一些会话数据进行保存,当应用恢复后同时将暂存数据复原,以便让用户继续使用。需要注意的是MSDN中提到:“当用户通过按 Alt+F4 或使用关闭手势关闭应用时,应用将被挂起 10 秒钟然后被终止。”也就意味着关闭的应用只有10秒钟时间可以被恢复。下面将通过实例进行演示,首先创建一个Textbox 让用户录入名字进行会话操作。我们首先来尝试一下没有进行挂起暂存处理的应用是何种结果。






挂起
  直接按F5运行应用,在Name 栏中输入名字或任意字符。在VS2012的Debug Location 工具栏可以看到挂起(Suspend )的选项,我们选择挂起并终止(Suspend and shutdown),程序挂起后从系统左侧菜单栏里找到之前的应用重新启用,恢复后的应用Name 栏中的文字已经丢失。对于名字这样的简单录入还可以接受,如果录入项较多的话那将损失惨重。
DSC0001.png
  接下来我们将进行应用挂起处理,打开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 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 pageState)
{
if (pageState != null && pageState.ContainsKey("name"))
{
nameInput.Text = pageState["name"].ToString();
}
}
源码下载
  http://sdrv.ms/U1zyQd

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69352-1-1.html 上篇帖子: 重新想象 Windows 8 Store Apps (28) 下篇帖子: Windows phone 8 学习笔记(3) 通信
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表