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种:

终止原因PreviousExecutionState 属性的值采取的操作已由系统终止(例如,因为资源限制)Terminated还原会话数据被用户关闭ClosedByUser 使用默认数据启动           意外终止,或者应用自从用户的会话开始以来未运行 NotRunning使用默认数据启动

 

下次启动时,如果上一次的状态为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();        }


注意:当应用程序被用户关闭时,5秒过后才会进入Suspending状态。如果用户在这5秒内重新启动应用,上一个应用程序会立即被关闭,然后重新启动新的应用程序。 那么上一个程序注册的Suspending事件就会失效。

所以,不能把保存应用程序信息的工作放在OnSuspending方法里。

 

 3.模拟挂起和恢复

在VS里打开“调试位置”工具栏,可以看到”挂起“图标。
我用的是繁体版的,截图如下:


 点击”挂起“,程序会进入Suspending状态,并调用OnSuspending方法。

点击”恢复“,程序会继续运行。

点击”挂起和恢复“,程序会以Terminated状态关闭。程序下次启动时会还原会话数据。

 

 

专栏网址:http://blog.csdn.net/zyc13701469860/article/details/8194090