Wednesday, December 19, 2012

MvvmCross + WinRT and default Page templates...

We've got an intern at my working place, which is doing some stuff on WinRT and wants to use some of the MvvmCross code I have written. So I sat down with him today to get him started. 
We got the model code to compile and some of the code, which used Threads instead of Tasks. However our biggest problem was not to get stuff to compile! What was the biggest problem was an issue where at run time, our ViewModel was never set in our Page. So we set up a ton of break points and I think we got through almost all aspects of how MvvmCross locates Views and ViewModels and how it navigates etc., to no avail and had to ask Stuart Lodge on the Jabbr chat for help. 

He gave us a hint about the OnNavigatedTo method in the LayoutAwarePage. The call to base.OnNavigatedTo(e) is what sets up the ViewModel on a page, it was never called! So we starred blindly into the code until we saw that the default template for a Page on WinRT overrides the OnNavigatedTo method itself, and the implementation is empty. So by removing that everything started to work! So be sure to call base.OnNavigatedTo in your Page implementation!