Mittwoch, 17. November 2010

Windows Phone 7: Handle Back button for a open popup

If you are pressing the back button in your Windows Phone 7 app with an opened popup, the NavigationService will leave the current page and navigate back to the previous page on the navigation stack.

To close the popup instead of navigating, you can use this: Handling the “Back” Button with a Popup Open on Windows Phone 7 | Toetapz's Blog

Sonntag, 14. November 2010

Windows Phone 7: Using threading for heavy operations

For developers of Windows Phone 7 apps it is important to know all existing concepts of scheduling tasks on Windows Phone 7. At Windows Phone 7 the core threads are the Compositor Thread which interacts with the GPU and the UI Thread. The UI Thread is the main thread, where all your code will normally be executed. But the UI Thread have to handle some important events of the OS and is responsible for the layout management. Therefore it is not a good idea to do heavy actions in the UI thread, because then your app will loose very quickly the “smooth” experience of Windows Phone 7.

The normal way is to create for this heavy operations (parsing JSON, rendering bitmaps, etc.) a new thread directly or using the  BackgroundWorker class. An alternative way you can found at

Scheduling tasks on Windows Phone 7 | .NET Zone.

Instead of using the direct ways for new thread creation, you could use the built-in scheduling mechanism which are in the namespace Microsoft.Phone.Reactive. The Scheduler class in this namespace allows you to defer execution of pretty much everything you need. You can either directly operate with Scheduler or via IScheduler, that will get a thread instance from Scheduler.

Samstag, 13. November 2010

Using authenticated RIA Services with Windows Phone 7

In my older post RIA Services: Windows Phone 7 and SOAP endpoint | .NET - Red zone : Best practices and latest stuff I have described how a RIA Service can be used as normal WCF SOAP service for example with Windows Phone 7.

But if you want to use RIA Services with Windows Phone 7, there is the problem that Silverlight for Windows Phone 7 does not support authentication headers. But using the instructions here Using Authenticated Ria Services on your WP7 phone - Marcel de Vries, MVP Team System - blog community you can still use the cool authorization mechanism of WCF RIA Services with Windows Phone 7.

The key steps are:

1. Add enableHttpCookieContainer="true" to your ServiceReferences.ClientConfig

After the proxy is generated enable the HttpCookieContainer at your bindings for authentication service and your domain service. You have to remove it for each proxy new configuration to avoid an error message during proxy generation.

   1: <bindings>
   2:             <basicHttpBinding>
   3:                 <binding name="BasicHttpBinding_AuthenticationServiceSoap" maxBufferSize="2147483647"
   4:                          enableHttpCookieContainer="true"
   5:                     maxReceivedMessageSize="2147483647">
   6:                     <security mode="None" />
   7:                 </binding>
   8:                 <binding name="BasicHttpBinding_EmployeeDomainServiceSoap" maxBufferSize="2147483647"
   9:                       enableHttpCookieContainer="true"
  10:                     maxReceivedMessageSize="2147483647">
  11:                     <security mode="None" />
  12:                 </binding>
  13:             </basicHttpBinding>
  14:         </bindings>
2. Store the returned cookie container of your Login-Call for the next service calls
   1: protected virtual void OnLoginCompleted(object sender, LoginCompletedEventArgs e)
   2:        {
   3:            var args = new LoginServiceCompletedEventArgs();
   4:            
   5:            CookieContainer = null;
   6:            
   7:            if (e.Result != null)
   8:            {
   9:                args.User = null;
  10:                args.Error = true;
  11:  
  12:                CookieContainer = _authclient.CookieContainer;
  13:                ViewModelLocator.AuthCookieContainer = _authclient.CookieContainer;

Hint: Don’t be surprised that the cookie container looks empty at this point (count is 0). There is a kind of “magic” cookie still in there.

3. Use the returned cookie container for your proteced domain service calls
   1: var client = new EmployeeDomainServiceSoapClient
   2:                  {
   3:                      CookieContainer = ViewModelLocator.AuthCookieContainer
   4:                  };

Donnerstag, 11. November 2010

Windows Phone 7: My Interview on Mobile360 and .NET Magazin

My interview (in german only) about developing Windows Phone 7 apps could be read at the Mobile360 – Website: Was Entwickler bei Windows Phone 7 beachten sollten or at .NET Magazin Was Entwickler ・er Windows Phone 7 wissen sollten.

Samstag, 6. November 2010

New Silverlight Toolkit for Windows Phone 7 available

The new November 2010 edition of the open source-based Silverlight Toolkit for Windows Mobile developers brings new pre-built controls that they can make your apps easily and refine. The new and exciting elements AutoCompleteBox, ListPicker LongListSelector (this control helps a lot), different page transition effects (page transitions) are now available in the current version. Of course the controls known from the previous version remain included in the scope of the Toolkit (GestureService GestureListener, ContextMenu, DatePicker, TimePicker, ToggleSwitch, WrapPanel).

You can download the toolkit here: Silverlight - Release: Silverlight for Windows Phone Toolkit - Nov 2010

Donnerstag, 28. Oktober 2010

WindowsPhone 7: NavigationService.Back raises Exception

Sometime a NavigationService.Back call could raise an exception, if the user tombstoned your Windows phone 7 app and you try to call a Navigations.Back after this.

Possible szenario: You ask the user “Do you really want to delete this item?”. After returning a OK from your MessageBox you delete the item and in all cases you want to return from your detail page to your list page with NavigationService.Back.  To avoid a exception you have to put your NavigationService.Back call in a try..catch statement, because the user could press the windows button at your Messagebox and after this the CLR will raises a Cancel for your messagebox and executes the rest of your code with the NavigationService.Back call. Guess from me: Because this situation is not handled by the Silverlight for Windows Phone framework a exception will occur.

Trauriges SmileyTrauriges SmileyTrauriges Smiley

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