ViewModel Location

Topics: Bootstrappers & IoC, Conventions, Getting Started
Sep 21, 2011 at 2:27 PM
Edited Sep 21, 2011 at 2:58 PM

I have a Windows Phone 7 application that was written using Caliburn Micro v1.0 that I'm trying to update to use v1.2 but I'm running in to a problem that's really bugging me and the views are not being bound to the viewmodels. In my application all of my view models are in the project.viewmodels namespace and the views are in the project namespace, but when I try and run it loads the mainpage.xaml view but it's not bound to any viewmodel. However, if I move the viewmodels in to the project namespace it will work fine.

I have created a simple demo application here: https://skydrive.live.com/?cid=344c552ff1338ebe&id=344C552FF1338EBE%21202# that highlights the issue, I'm guessing that I've got something wrong with my naming conventions but any help would be appreciated!

Cheers!

Coordinator
Sep 21, 2011 at 2:51 PM

We've been making some fixes recently. Can you please try the latest code and see if your issue is still present?

Sep 21, 2011 at 3:14 PM

I've just tried it with changeset 6636e9b93700 and I have the same problem

Sep 21, 2011 at 4:29 PM

The convention is as follows:

Project.ViewModels.SomeViewModel => Project.Views.SomeView

or

Project.SomeViewModel => Project.SomeView

You will need to override the default behavior by adding your own rule in the boot strapper:

ViewLocator.NameTransformer.AddRule
(
    @"(?<nsbefore>([A-Za-z_]\w*\.)*)(?<nsvm>ViewModels\.)(?<nsafter>([A-Za-z_]\w*\.)*)(?<basename>[A-Za-z_]\w*)(?<suffix>ViewModel$)",
    @"${nsbefore}${nsafter}${basename}View",
    @"([A-Za-z_]\w*\.)*ViewModels\.([A-Za-z_]\w*\.)*[A-Za-z_]\w*ViewModel$"
);

This is a very simple modification to the existing rule to omit the "Views" from the derived namespace. So while this will cover your existing use case:

Project.ViewModels.SomeViewModel => Project.SomeView

It will also resolve the name in the following way should you further nest the namespace for your ViewModels in the future:

Project.ViewModels.SomeNameSpace.SomeViewModel => Project.SomeNameSpace.SomeView

 

Sep 22, 2011 at 8:36 AM

Thanks for your help guys, I didn't have a great deal of joy with it but restructuring my project to put all of my views within the project.Views namespace solved the problem.