Donnerstag, 28. Oktober 2010

Windows Phone 7: Tombstoning and Messagebox

For a good usabilty for your Windows Phone 7 app you should consider to save your Messagebox text as transient data for example with help of the PhoneApplicationService.Current.State Dictionary. Why is this necessary? For example if the user gets a call or wants to switch to a other ap by pressing the windows button and have not read the Messagbox the user should shown it again after he return to your app.

We use in our project the following class which do all the work for us.

The Show-methods wrapps the orginal MessageBox-Show methods (one override is missing, because a show method with return value for questions make no sense. After tombstoning you loose your functional context, so this Messagebox type should be handled). The ShowFromTransientValues-Method is the key for the suggested behaviour and can be called after you bring your application in a proper state – for example in the first PageLayoutUpdated-Event after reactivation.

HINT: The MessageBox.Show method will return in case of tombstoning always a cancel value. That is a fact that you should keep in mind for Windows Phone 7 programming.

   1: public static class TransientMessageBox
   2:     {
   3:         const string MessageTextKey = "TransientMessageBoxText";
   4:         const string MessageCaptionKey = "TransientMessageBoxCaption";
   5:  
   6:         public static void ShowVirtual(string messageBoxText)
   7:         {
   8:             TransientStorageHelper.AddOrUpdate(MessageTextKey, messageBoxText);
   9:         }
  10:  
  11:         public static void ShowVirtual(string messageBoxText, string caption)
  12:         {
  13:             TransientStorageHelper.AddOrUpdate(MessageTextKey, messageBoxText);
  14:             TransientStorageHelper.AddOrUpdate(MessageCaptionKey, caption);
  15:         }
  16:  
  17:         public static void Show(string messageBoxText)
  18:         {
  19:             ShowVirtual(messageBoxText);
  20:  
  21:             var result = MessageBox.Show(messageBoxText);
  22:  
  23:             Debug.WriteLine("Transient MessageBox result:" + result);
  24:  
  25:             if (result == MessageBoxResult.OK)
  26:             {
  27:                 RemoveTextKey();
  28:             }
  29:         }
  30:  
  31:         public static void Show(string messageBoxText, string caption)
  32:         {
  33:             ShowVirtual(messageBoxText, caption);
  34:  
  35:             var result = MessageBox.Show(messageBoxText, caption, MessageBoxButton.OK);
  36:  
  37:             Debug.WriteLine("Transient MessageBox result:" + result);
  38:  
  39:             if (result == MessageBoxResult.OK)
  40:             {
  41:                 RemoveAllKeys();
  42:             }
  43:         }
  44:  
  45:         private static void RemoveAllKeys()
  46:         {
  47:             TransientStorageHelper.Remove(MessageTextKey);
  48:             TransientStorageHelper.Remove(MessageCaptionKey);
  49:         }
  50:  
  51:         private static void RemoveTextKey()
  52:         {
  53:             TransientStorageHelper.Remove(MessageTextKey);
  54:         }
  55:  
  56:         public static void ShowFromTransientValues()
  57:         {
  58:             if (!PhoneApplicationService.Current.State.ContainsKey(MessageTextKey)) return;
  59:  
  60:             if (PhoneApplicationService.Current.State.ContainsKey(MessageCaptionKey))
  61:             {
  62:                 if (MessageBox.Show(TransientStorageHelper.GetValue<string>(MessageTextKey),
  63:                                 TransientStorageHelper.GetValue<string>(MessageCaptionKey),
  64:                                 MessageBoxButton.OK) == MessageBoxResult.OK)
  65:                 {
  66:                     RemoveAllKeys();
  67:                 }
  68:             }
  69:             else
  70:             {
  71:                 if (MessageBox.Show(TransientStorageHelper.GetValue<string>(MessageTextKey)) == MessageBoxResult.OK)
  72:                 {
  73:                     RemoveTextKey();
  74:                 }
  75:             }
  76:         }
  77:     }

Keine Kommentare:

Kommentar veröffentlichen