Windows8 Metro开发 (01) : Metro应用程序生命周期
来源:互联网 发布:用php做一个表格 编辑:程序博客网 时间:2024/06/10 04:06
Josè Mourinho的专栏: http://blog.csdn.net/zyc13701469860
转载请注明原作者和出处。
Metro应用程序生命周期
Metro应用程序只有3个状态:
- Not Running
- Running
- Suspend
Not Running状态:
应用程序进程刚刚部署、出现故障停止、或者被挂起但无法保留在内存中。
Running状态:
应用程序正常运行。
Suspend状态:
用户离开程序
因电量不足而被系统挂起
用户关闭程序(先进入Suspend状态,内存销毁后进入Not Running状态)
状态图如下:
在Suspend状态的时候,系统会为用户保存当前程序会话数据。
应用程序恢复:
程序终止的原因有以下3种:
下次启动时,如果上一次的状态为Terminated,就还原会话数据,显示上次程序关闭前的内容。
代码分析
1.创建一个metro应用程序。
如果你还没有配置好开发环境,请参考 老周的博客:新时尚Windows8开发(1):如何创建应用程序项目。
2.打开App.xaml.cs文件
在构造函数App()里会注册Suspending事件。
public App() { this.InitializeComponent(); this.Suspending += OnSuspending; }
程序会通过OnLaunched方法启动,可以通过参数arg的PreviousExecutionState属性来获取前一次程序的终止原因。
如果终止原因为ApplicationExecutionState.Terminated就会调用await SuspensionManager.RestoreAsync();方法还原会话。
PS:DebugUtil是我封装的一个类,输出debug信息请用Debug.WriteLine()方法。
protected override async void OnLaunched(LaunchActivatedEventArgs args) { DebugUtil.WriteLine(DEBUG_FLAG,string.Format("程序前一次状态为: {0}", args.PreviousExecutionState.ToString())); 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 page rootFrame = new Frame(); //Associate the frame with a SuspensionManager key SuspensionManager.RegisterFrame(rootFrame, "AppFrame"); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) { // Restore the saved session state only when appropriate try { DebugUtil.WriteLine(DEBUG_FLAG, "还原上一个应用程序的会话数据"); await SuspensionManager.RestoreAsync(); } catch (SuspensionManagerException) { //Something went wrong restoring state. //Assume there is no state and continue } } // Place the frame in the current Window Window.Current.Content = rootFrame; } ... Window.Current.Activate(); }
当程序进入Suspending状态时,会调用OnSuspending方法,保存当前会话状态。
private async void OnSuspending(object sender, SuspendingEventArgs e) { DebugUtil.WriteLine(DEBUG_FLAG, "OnSuspending"); var deferral = e.SuspendingOperation.GetDeferral(); await SuspensionManager.SaveAsync(); deferral.Complete(); }
所以,不能把保存应用程序信息的工作放在OnSuspending方法里。
3.模拟挂起和恢复
点击”挂起“,程序会进入Suspending状态,并调用OnSuspending方法。
点击”恢复“,程序会继续运行。
点击”挂起和恢复“,程序会以Terminated状态关闭。程序下次启动时会还原会话数据。
专栏网址:http://blog.csdn.net/zyc13701469860/article/details/8194090
- Windows8 Metro开发 (01) : Metro应用程序生命周期
- 【windows8开发】Metro App生命周期之概述
- 【windows8开发】Metro App生命周期之启动
- Windows8 Metro开发 -- 总览
- 【windows8开发】Metro App生命周期之Suspended与Resume
- Windows8 Metro开发 (04) : 保存/读取本地应用程序设置
- Windows8 Metro开发 (04) : 保存/读取本地应用程序设置
- 【windows8开发】C++开发Metro风格App
- 【windows8开发】javascript开发Metro风格App
- Windows8 Metro style app开发前瞻
- windows8开发-metro应用之Popup窗口
- 【Windows8开发】Metro App生命周期之打包、部署安装与卸载
- 基于VisualStudio11开发Windows8的Direct2D-Metro应用程序范例(1)hello world
- 基于VisualStudio11开发Windows8的Direct2D-Metro应用程序范例(2) 国际化文字展示
- 跟燕青一起学Windows8应用开发:创建HTML5 Metro应用程序
- Windows8 Metro快捷键大全
- Windows8 Metro 初体验
- win8下metro风格应用程序的生命周期
- 你笑凤姐太疯癫,凤姐笑你看不穿
- SGI STL空间适配器freelist设计
- oracle quota
- linux下对sh文件的基本操作总结
- Struts2 环境配置(struts2.3.4)
- Windows8 Metro开发 (01) : Metro应用程序生命周期
- linux下单机安装hadoop
- 多线程工作
- Android服务的生命周期
- 1602液晶模块使用归纳
- vim中的窗口分割
- n个小孩围城一圈数数,数到 2^k, 就退出圈子。07年 C 语言最后一道![专硕]@NEU
- public class Square;square.draw(gl); // ( NEW )
- tomcat 登录用户名 与 密码设置