zoukankan      html  css  js  c++  java
  • 使用C#开发Metro 风格应用的路线图 metro应用生命周期的处理

    我们知道,程序可能有活跃,扶起,激活,终止等状态。

    这里主要讲的是当用户切出应用又切回来时需要特别地做的一些处理, 与wp7相同,例如:

    微软有一些用户体验的指导方针:

    • 当用户恢复应用时我们要保证和离开应用时的效果一样,我们需要处理 浏览器session, 购物车,未完成的输入,正在进行中的电影或游戏 等之类的数据。
    • 当用户再次启动应用程序时要记录用户已经浏览过的数据,避免重复浏览,比如 新闻的条数,天气的日期。
    • 当程序挂起时记录程序数据,因为挂起的终止时不再接受notification
    • 当程序从挂起恢复时,最好更新一下UI,因为有些数据可能在后台更新了.
    • 当程序从终止重新启动时,从存储里恢复上次的数据
    • 让用户选择重新开始或恢复上次的数据

    1、当程序激活时我们需要做什么

      重写OnLaunched事件

    using System;
    using Windows.ApplicationModel;
    using Windows.ApplicationModel.Activation;
    using Windows.UI.Xaml;
    
    namespace AppName
    {
       public partial class App
       {
          async protected override void OnLaunched(LaunchActivatedEventArgs args)
          {
             EnsurePageCreatedAndActivate();
          }
    
          // Creates the MainPage if it isn't already created.  Also activates
          // the window so it takes foreground and input focus.
          private MainPage EnsurePageCreatedAndActivate()
          {
             if (Window.Current.Content == null)
             {
                 Window.Current.Content = new MainPage();
             }
    
             Window.Current.Activate();
             return Window.Current.Content as MainPage;
          }
       }
    }

        恢复数据

    async protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
       if (args.PreviousExecutionState == ApplicationExecutionState.Terminated ||
           args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
       {
          // TODO: Populate the UI with the previously saved application data
       }
       else
       {
          // TODO: Populate the UI with defaults
       }
    
       EnsurePageCreatedAndActivate();
    }

        如果程序是非法终止的,即是PreviousExecutionState状态是  NotRunning, 那就没有数据可以恢复。

    2、当程序挂起时我们需要做什么

        首先订阅suspending事件

    using System;
    using Windows.ApplicationModel;
    using Windows.ApplicationModel.Activation;
    using Windows.UI.Xaml;
    
    partial class MainPage
    {
       public MainPage()
       {
          InitializeComponent();
          App.Current.Suspending += new SuspendingEventHandler(App_Suspending);
       }
    }

       然后保存用户数据

    CoreDispatcher dispatcher = Window.Current.Dispatcher;
    
    private void App_Suspending(object sender, object e)
    {
        // This is a good time to save app data in case the process gets terminated.
        IPropertySet settingsValues = ApplicationData.Current.LocalSettings.Values;
    
        // TODO: Save the app data
    }

        当程序挂起时数据会保存在内存,但是当系统资源不够用时系统会终止程序,所以当我们重新启动程序时,系统会触发Activated事件,我们需要在OnLanuched方法中恢复上次挂起时保存的数据。系统并不会通知我们何时终止了程序, 所以我们必须得在挂起时保存数据以保证在重新启动终止的程序时能恢复数据。

    3、当程序恢复时我们需要做什么

        首先订阅Resuming事件

    partial class MainPage
    {
       public MainPage()
       {
          InitializeComponent();
          App.Current.Resuming += new Windows.UI.Xaml.EventHandler(App_Resuming);
       }
    }

        更新UI可能产生的新数据

    CoreDispatcher dispatcher = Window.Current.Dispatcher;
    
    private void App_Resuming(object sender, object e)
    {
        // There are no special arguments for the resuming event
        dispatcher.Invoke(CoreDispatcherPriority.Normal, 
          (object invokedSender, InvokedHandlerArgs invokedArgs) =>
        {
            // TODO: Refresh network data
        }, this, null);
    }

     如果没有数据要更新,则不需要订阅此事件。

  • 相关阅读:
    jQuery EasyUI API 中文文档 数字框(NumberBox)
    jQuery EasyUI API 中文文档 数值微调器(NumberSpinner)
    jQuery EasyUI API 中文文档 日期时间框(DateTimeBox)
    jQuery EasyUI API 中文文档 微调器(Spinner)
    jQuery EasyUI API 中文文档 树表格(TreeGrid)
    jQuery EasyUI API 中文文档 树(Tree)
    jQuery EasyUI API 中文文档 属性表格(PropertyGrid)
    EntityFramework 数据操作
    jQuery EasyUI API 中文文档 对话框(Dialog)
    jQuery EasyUI API 中文文档 组合表格(ComboGrid)
  • 原文地址:https://www.cnblogs.com/icuit/p/2478222.html
Copyright © 2011-2022 走看看