Important fix for WP7 devs

Jul 14, 2010 at 4:23 AM
Edited Jul 14, 2010 at 4:26 AM

I have been experimenting with Caliburn.Micro on my WP7 projects. For days I have been facing an extremely frustrating bug whenever I used an ItemsControl on my view and used Convention binding on it.

 

I finally traced the problem to the automatically generated DataTemplate that caliburn uses for the ItemsTemplate.

 

If you open the ConventionManager.cs you will find the following code, which is using the assembly-level attributes to load the Caliburn namespace via XML namespace http://www.caliburnproject.org -- Unfortunately, it seems WP7 does not like this.

 

 

 

 "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
                          "xmlns:cal='http://www.caliburnproject.org'> " +
                "<ContentControl cal:View.Model=\"{Binding}\" VerticalContentAlignment=\"Stretch\" HorizontalContentAlignment=\"Stretch\" />" +
            "</DataTemplate>"

 

 

 

 

I changed the namespace to use the standard clr-namespace syntax, and Caliburn started working again.

 

"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +  

"xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " +

 "xmlns:cal='clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro'> " +   

"<ContentControl cal:View.Model=\"{Binding}\" VerticalContentAlignment=\"Stretch\" HorizontalContentAlignment=\"Stretch\" />" +  

"</DataTemplate>"

 

 

 

I'm really not sure if this problem is unique to my project or not, Rob perhaps you could give it a shot if you have a WP7 sample somewhere? The problem is easy to reproduce if you simply have a IEnumerable<ViewModel> property and an ItemsControl in your view.

The exceptions I received were very random internal MS exceptions.

 

Hopefully this helps someone. Please let me know if I can add additional information.

 

-Matt

 

 

 

 

 

 

 

Coordinator
Jul 14, 2010 at 12:37 PM

Thanks for tracking this down. I hadn't tested this specific scenario with WP7. I forgot that WP7 is based on SL3 which did not support uri namespace declarations. I'll get this fixed shortly.

Coordinator
Jul 14, 2010 at 12:58 PM

Fixed.

Jul 14, 2010 at 2:52 PM

I have problems using Caliburn.Micro with the new Beta tools, as Microsoft has mandated that views must be contained within a PhoneApplicationPage.  I'm new to Caliburn, so I'm not sure where I need to look to fix it myself and report my fix.

Jul 14, 2010 at 3:09 PM

@MarlonDSmith,

I haven't had any problems using a custom ShellViewModel as my RootVisual, however, there are some tweaks you need to make.

Going from memory here, the first thing I did, was open my WMAppManifest.xml, there is a <Tasks Name="_defaultTask" NavigatePage="Mainpage.xaml" />

I simply removed the NavigatePage attribute all together, this let me handle the app init and page loading myself. Also, there is another Discussion where someone mentions an issue with the App.xaml.cs that is created my default, where is sets the RootVisual in the PhoneInitialization() method. Perhaps have a look at that too.

 

Hope that helps,

Matt

Jul 14, 2010 at 3:12 PM

Thank you so much dude, removing that attribute from the app manifest did the trick.

Jul 14, 2010 at 3:39 PM

Great, glad to hear it helped! Learning the in's and out's of a new (and beta) platform can be pretty trying on everyone's patience at times :)

Jul 14, 2010 at 9:48 PM

One other question for you.  How are you managing navigation?  Are you injecting the NavigationService into your VM or Controller?

Jul 14, 2010 at 9:56 PM

Right now I'm failing miserably at navigation actually. I am trying to port my app from Rob's Build your own MVVM framework (which has the Show.Screen<>) helpers, and navigation IResult's. Right now I'm really looking for guidance on how to do with navigation with Caliburn.Micro.

Sorry I couldn't really help, I will followup if I come up with a good solution.

-Matt

Coordinator
Jul 15, 2010 at 1:38 PM

Can you provide some more details with exactly what area you are getting stuck on?

Jul 16, 2010 at 2:27 PM

Sure, I've dug around a bit more and there are two places where I am stcuk.  I'm new to caliburn so please excuse my terms or explinations.

First, I recreated your sample from part 1 on WP7 and the data binding textblocks work but the "Can" convention and button trigger does not. 

Secondly, I've been struggling to figure out how I want my VM to aquire an instance of the NavigationService and I'd like to avoid using code behind in my pages.

Any suggestions?

Thanks

Coordinator
Jul 16, 2010 at 3:43 PM

You know... I totally forgot that you sent me a solution to look at. Let me dig into it this weekend. I need to get the next CM article out, then I will look into your sample. If you've made any significant changes, feel free to send me an updated version.

Jul 18, 2010 at 10:25 PM

I have also been playing around a bit with taking some of the concepts from the 'Build your own MVVM' talk and implementing them in Caliburn.Micro. Here is what I came up with for navigation:

I have my ShellViewModel derive from Conductor<IScreen> which allows me to call ActivateItem on a given ViewModel to navigate to it's related view. So in the GameLibray example where it has:

 

 

public class ShellViewModel : ScreenConductor, IShell
{
    public ShellViewModel(SearchViewModel firstScreen)
    {
        OpenScreen(firstScreen);
    }
}

 

You could write this in Caliburn.Micro as:

 

public class ShellViewModel : Conductor<IScreen>
{
    public ShellViewModel(SearchViewModel firstScreen)
    {
        ActivateItem(firstScreen);
    }
}

This seems to work for me. I am not 100% sure this is the correct way.

 

Coordinator
Jul 19, 2010 at 2:07 AM
That's the way :)
Jul 19, 2010 at 8:37 PM

The navigation stuff in WP7 is slightly awkward in that it's view first and you can't change the "content loader".

 

In the end my solution was to create a sub class of PhoneApplicationFrame.

 

This uses a ViewModelLocator and ViewModelFactory to create the appropriate ViewModel and Bind it to the already created view

 

One thing I like about this solution is that I can inject the query string parameters into the ViewModel.

 

Jul 19, 2010 at 9:07 PM

@nigelsampson - I would love to see a code example of that if you have time.

Thanks,

Luke

Coordinator
Jul 19, 2010 at 11:38 PM

@nigelsampson

Are you in a position where you can share the code for that? It might be a good candidate for the WP7 Bootstrapper. I'm going to be trying to improve the WP7 stuff in the next week or so. I'd love to have your solution to consider. Much thanks!