使用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);
}

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

代码介绍 MetroForWinForm(win8风格模版) using System; using System.Drawing; using System.Globalization; using System.Windows.Forms; using MetroFramework.Forms; namespace MetroFramework.Demo { public partial class MainForm : MetroForm { public MainForm() { InitializeComponent(); metroStyleManager.Theme = MetroThemeStyle.Default; metroStyleManager.Style = MetroColorStyle.Teal; } private void metroTileSwitch_Click(object sender, EventArgs e) { var m = new Random(); int next = m.Next(0, 13); metroStyleManager.Style = (MetroColorStyle)next; } private void metroTile1_Click(object sender, EventArgs e) { metroStyleManager.Theme = metroStyleManager.Theme == MetroThemeStyle.Light ? MetroThemeStyle.Dark : MetroThemeStyle.Light; } private void metroButton1_Click(object sender, EventArgs e) { MetroTaskWindow.ShowTaskWindow(this, "SubControl in TaskWindow", new TaskWindowControl(), 10); } private void metroButton2_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "Do you like this metro message box?", "Metro Title", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk); } private void metroButton5_Click(object sender, EventArgs e) { metroContextMenu1.Show(metroButton5, new Point(0, metroButton5.Height)); } private void metroButton6_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` only button", "MetroMessagebox", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void metroButton10_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `OK` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } private void metroButton7_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes` and `No` button", "MetroMessagebox", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } private void metroButton8_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Yes`, `No` and `Cancel` button", "MetroMessagebox", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); } private void metroButton11_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Retry` and `Cancel` button. With warning style.", "MetroMessagebox", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning); } private void metroButton9_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample MetroMessagebox `Abort`, `Retry` and `Ignore` button. With Error style.", "MetroMessagebox", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error); } private void metroButton12_Click(object sender, EventArgs e) { MetroMessageBox.Show(this, "This is a sample `default` MetroMessagebox ", "MetroMessagebox"); } private void metroButton4_Click(object sender, EventArgs e) { var testform = new TestForm1(); testform.ShowDialog(); } private void metroButton4_Click_1(object sender, EventArgs e) { metroTextBox2.Focus(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值