Handle Windows Phone back button pressed when using MVVM

When I building an app with a MVVM framework I want to have my view models in a portable class library so I can use same view models for the Windows Phone app as for the iOS and Android app when building apps using Xamarin.

If I press the back button in a Windows Phone app built using MvvmCross (a MVVM framework), the app will be placed in background. That is the behavior I want if I'm on the first view of the app. But if I have navigated to Another view I would like to return to the previous view. To do that I need to handle the back button event. I want to have the code for handle the back button event in my view models that is placed in a portable class library, but the code for handling the event is platform specific. This post will show how to handle the back button event in a Windows Phone app built using WinRT. You can of course use this way to handle platform specific events for other events than the back button event.

First of all I creating an interface, I use to name it IPlatformEvents.

public interface IPlatofrmEvents
     event EventHandler BackButtonPressed;

In my view models I using IoC (Inversion of Control) and constructor injection to resolve the interface. In my method for handling the back button pressed I using the Close method to close the current view and return to the previous one in the stack. Don't forget to remove the handler from the event in the handler method. If you do not, it can cause problems later.

public class MyViewModel : MvxViewModel
     private IPlatformEvents _platformEvents;     
     public MyViewModel(IPlatformEvents platformEvents)
          _platformEvents = platformEvents;
          _platformEvents.BackButtonPressed += BackButtonPressed;
     void BackButtonPressed(object sender, EventArgs e)
         _platformEvents.BackButtonPressed -= BackButtonPressed;

If you want to read more about IoC and MvvmCross you can read my post about MvvmCross and IoC and my post about how to use a different IoC provider then the built-in when using MvvmCross.

Next step is to write the platform specific implementation of the interface. For an Windows Phone app using WinRT the implementation would look like this.

public class WindowsPhoneEvents : IPlatformEvents
      public event EventHandler BackButtonPressed;
      public WindowsPhoneEvents()
          HardwareButtons.BackPressed += HardwareButtons_BackPressed;
      void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e) 
             if(BackButtonPressed != null) 
                   BackButtonPressed(this, new EventArgs()); 
                   e.Handled = true; 

When I have created the platform specific implementation I just have to register it to the IoC container. You can read how to do it if you read the post that I linked to above.

Running Windows Phone emulator in Parallels on OS X

If you're developing apps for all the major mobile platforms i guess you are using a Mac since that is the only way you can build iOS apps. If you're using Parallels to run Windows for Windows Phone development you need to activate nested virtualization if you want to use the emulator. This is because that Windows Phone emulator is using virtualization with Hyper-V.

To activate nested virtualization, shut down your virtual machine if it is started. Then open the configuration view for your machine in parallels and go to the "Optimization tab" and check "Enable nested virtualization". When you start your virtual machine you'll be able to use Hyper-V and the Windows Phone emulator.

Configuration dialog for virtual machine

Travelling with my Lumia

For a couple of weeks was me, my girlfriend and a friend visiting Unites States, we visiting New York and drove around by car at the west side of the country. Under the two weeks we was there we travelled over 400 miles by car.

To navigate we used my Nokia Lumia 920 and the Here Drive+ apps that comes with all Nokia Windows Phones. I have downloaded all maps before we left Sweden.

I was a little bit unsure how it will worked before, but people on a Swedish group for Windows Phone users told me that it will work fine. I have used it in Sweden with good result earlier but I asked for to be sure. All we talked about before told us it was a good idea to bring a GPS.

The people on the Windows Phone group had right it worked very well, I am impressed that a free app could work so good (it coast a few dollar if you do not have a Nokia, but it definitely worth it). We used it not just when we went by car, but also when we walking in big cities like New York.

To have something to attach the phone to I brought my Nokia CR-200 holder with build in wireless charging that I bought earlier this summer.

To use my Windows Phone instead of renting a GPS, we saved pretty much money that we could use for other things.

If you is to buy a new phone, just Here Drive+ is a reason enough to buy a Windows Phone.


Nokia CR200

Beyond the Tiles

Monday was me, Johan (@johankson) and David(@d_andersson) visiting Microsoft for a seminar about design, primary for Windows 8 and Windows Phone. Arturo Toledo (@arturot), Alex Toledo (@toledoal) and Vincent Garcia (@vincentgarcia) was the speakers at the seminar. Arturo is a former member of Microsofts Windows Phone team but now he has his own company, Toledo2, together with his brother Alex. The third speaker Vincent Garcia are working with design for Windows 8 and Windows Phone at Avanade Digital.

The Toledo brothers start the day by giving us a lesson in design history and talked about the Swiss design school and the international style. It's there the Metro design or Microsoft design language as it's called today, has its roots.

One of the message of the day that Microsoft design language is much more than tiles, thereof "Beyond the Tiles". They say the developers of Windows apps follow the guidelines to much and that make all Windows apps look the same, many developers use the Visual Studio templates and thinks the design has to be in that way.

Vincet Garcia showed us a couple of designs he had done for Windows 8 and Windows Phone and how the design has evolve with their increased understanding of the design language.

We also get som tip how we could improve the design of the Windows 8 version of WordRoom, thanks for that!

It was a good day and I became inspired and get much new knowledge to use in coming designs.