Tech Blog

Jay's Technical blog

An MVVM Light T4 template to auto build your ViewModelLocator

13 September 2010
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

A few weeks ago I tweeted that I had built a T4 template for auto-building your ViewModelLocator class After using it for a bit I have decided to finally release to more than just Laurent Buigion.

You will need to do a little bit of customization for each project. Take a look at lines 28-35

  1:string LocatorsNameSpace = "Namespace"; // This is the Namespace that your locator will appear. This is typically {AppName}.Views
  2:string LocatorClassName = "ViewModelLocator"; // This is the name of your locator class (this is the standard name)
  3:  vmsToExclude = new typeRef[] {
  4://new { typeName="MainViewModel"}   // Add entries here to exclude classes from your locator
  5:  };
  6:  baseClassNames = new typeRef[] {
  7:new typeRef{ typeName="GalaSoft.MvvmLight.ViewModelBase"}, // Add the base type(s) that your viewmodel's inherit from
  8:  };

I think this is pretty self-explanatory, but you need to –at the very least—set the namespace of the locator class.You might optionally want to change the location class’ name.

Lines 30-32 (3-5 above) let you override and tell the template to ignore certain classes.

Lines 33-35 (6-8 above) let you tell the engine what the parent class is of your ViewModels. If you are using MVVM Light then this should be correct. If you have your own parent class then you will want to use that instead.

This T4 template doesn’t currently search assemblies. Eventually I will do that, but for most of my work, the ViewModels are in my code and compiled with the project.

If you install the Chirpy addin, you can forget about the locator’s generation entirely as Chirpy will automatically run your T4 templates every compile.

Oh yeah, here’s the file

MEF For WP7 Update

07 August 2010
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

The other day I unveiled a build of MEF for WP7. Today, I made an update to my T4 template which now let’s it (the T4 template) see into assembly references and as a result you can now Compose types that are exported from other assemblies. I need to do some testing with my version now. I think I might actually be able to handle metadata and much of the things that “don’t work” in WP7 for MEF. I know that Microsoft will  eventually give us reflection support in WP7 and when that happens all this will be unnecessary, but for now I think we are in business.

Before I forget here’s the file - MEF2 Preview1 for wp7 build2

Added Benefits – for MVVM Light

Now that I have this working and I understand it. I plan on taking my MVVM Light ViewModelLocator generator T4 template and will be greatly enhancing it. I mean how would you like to be able to create a ViewModelClass inherit from the ViewModelBase, and have that locator generated for you without touching any other file. So you would create ViewModels and create views with associations to ViewModels and forget about the locator (except to wire it up in the app.xaml).

Drag/Drop check list for Jay..

06 August 2010
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

Ok, this is a personal post, but you can feel free to listen in. I needed a place to put this so I can troubleshoot a problem it seems I have every time I set up to use drag/drop functionality in Silverlight. You would think this should be easy for me since I wrote a control several months ago and I simply drop the control in, right?

Well, not so much. I’m always forgetting certain things (so I’m writing myself a check list.. mostly this is a stupid post.. but sometimes I’m a little dense); oh yeah, not only am I always forgetting things.. but I can’t ever not touch something..

Drag/Drop file troubleshooting

  • Is your viewmodel connected to the Xaml? Are you sure?
  • Is it connected in the ViewModelLocator? Are you sure? (man, it would be great if there were an easy way to do this.. too bad that experiment with this control and MEFedMVVM and MVVMLight didn’t work out)
  • Did you make modifications? Are you sure everything is still hooked up? (I bet you messed something up.. it should work out of the box, Jay.. BTW, why did you mess with it.. leave it alone it works good)
  • Is the AllowDrop property still in place?
  • You sure you didn’t mess with something?
  • Try to run in release mode (you’ve run into weirdness with trying to debug drag/drop.. and I’m not sure it works in non-release mode)
  • Try dragging/dropping from the My Documents folder instead of the desktop or wherever else you might be trying
  • BTW, did you change anything at all? (I know you.. you can’t keep your hands off code trying to make it better.. stop that! It works great!)

[I’ll probably add to this]

MEF2 Preview For WP7

03 August 2010
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

I’m pretty pumped.. I figured out how to hack my way around a couple issues to get WP7 to get MEF working. I also figured out how to get the CompositionInitializer working as well. Mostly I removed all the reflection stuff via “#if !WINDOWS_PHONE” directives and the in the “#else” I provide  alternative source. I honestly have barely tested it so be aware (but it should be fun to play with).

Silverlight Dev !=WP7 Dev

I mentioned that I hacked my way to getting the CompositionInitializer working (which is part of the source code in the last preview for MEF 1). The biggest problem is that you can’t walk the assemblies in Silverlight; in Silverlight you do walk the deployment package and load each assembly (which they are probably already loaded so this becomes “no operation” that returns a reference to the already loaded assembly). All of this is fine and well EXCEPT WP7 doesn’t allow you to load assemblies (part of the security model). You can inherit from Assembly –and the other reflection stuff—but you can’t instantiate any of this.

T4 to the rescue

So I created a T4 template that lets C# developers (sorry VB guys.. I’ll get to you soon) walk the project references associated with the main project and it finds a public type that can be used at runtime to get a reference to the underlying Assembly and adds this assembly to my StaticAssemblyResolver class which provides a list of Assemblies in place of the Assembly walking code; this is the default for my WP7 version of MEF.

How do I use it

I wanted to minimize the effort here. so you have to add a single line of code in your App’s startup code:

  1: System.ComponentModel.Composition.StaticAssemblyLoader.setup();

The only other thing you need to do is include my T4 template which is located in the Samples\WP7-SimpleViewModel project. (the T4 template might need to be run after adding a new project, but it should make all your Source Project assemblies available).

This should cover most of you. ext up for me will be to make the T4 work produce VB code as well as I want it to be able to walk the DLLs that are added to the project and get those assemblies added.

I have actually started trying to get some of the other MEF samples working as well (although a number of things will never work).

BTW, the source is still a little rough around edges for some of the stuff I’ve been actively working on.. much of the core was pretty simple.. it’s the stuff where I had to figure out how to help MEF find assemblies or get around WP7 reflection limitations.

Here’s the bits: MEF 2 Preview1 For WP7

Hooters Mosaic: How it Was Done Part 1 - Overview

02 July 2010
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

This is the first post in my series on how I built the Hooters mosaic. It wasn’t entirely me; I did have a team of designers helping me not to mention the awesome team of .Net developers who built the ASP.NET MVC site which actually does the work; I like to think of the mosaic as the icing on the cake, but what they did was the “steak” part of the project without their work there would be very little point to the whole project.

In this post I’ll give you the reader the background info on what you need to do you get started building a mosaic. Much of this blog series will focus on the other stuff in the project and not necessarily about how to build mosaic. This will be the only post that has anything to do with the mosaic building process. Here it is zoomed out if you haven’t seen it:

Hooters Mosaic (Hooters Logo made of Hooters-related pics) 


I know that one doesn’t normally start with the references, but the very first part of all this is that you need to know how to produce a mosaic.

  • The first and most important link you should look at is this one: NZ Synth.
  • The video in that link recommends using AndreaMosaic (which is what I used) to create a mosaic(if you want a searchable mosaic you’ll need this too); BTW, you want to spend the money on the Pro version… nd you want to mess with the megapixel settings (forget trying to figure everything else.. simply tell it “1000 megapixel” –or more with a PPI of 600)
  • He also mentions (in a blog post) the command-line utilities of HDView/HDMake (from MS Research) to generate your DeepZoom files (I recommend that as well.. I found DeepZoom composer was unable to deal with the extremely large image that I had produced). I actually created a .Bat file and had that plus the command-line utility in a zip file that dropped in with the image and ran.. it automatically produced a simple web page that hosted a generated silverlight app I could use to browse the mosaic locally.
  • The only other things you’ll need is lot’s of images (and if you want a decent looking mosaic you want to use 5000 images at a minimum.. AndraMosaic will re-use your images if you tell it, but that gets a little difficult to get right)
  • Oh yeah, you need to practice using AndreaMosaic. Trust me getting the settings right takes patience and a lot of practice.
  • After you have used all that you have a way to find images in your mosaic as well as you have a Deep Zoom folder with files that can be connected to the MultiScaleImage control (we will be talking about that in a later post).

If you haven’t figured it out the answers to how to build a mosaic are mostly out in the wilds of the Internet. Study the NZSynth info and source code and that will get you headed in the right direction.


I mentioned earlier that the image is large. Well, if you do it right, you’ll have a 1+gb image (our source image was around 1.6gb). Our image is 54000 x 37000 (it’s actually a little bigger than that.. I’m estimating). We used 1000 images for the mosaic and had it duplicate so we had over 10000 images (so images were duplicated many, many times). We also let the AndreaMosaic app play with the colors considerable because when you land on an actual girl we pop up the original image. In a future post we will explore the coordinate system in the MultiScaleImage (MSI) control.

Additionally if you monitor the network traffic you will see that we don’t do any actual service calls except for images because the data is all contained within the application. I used T4 templates to parse the various pieces of data and push it into programming constructs, so were able to use LINQ against classes in the UI to do searches against the data that we show on screen which means we could have made a richer search mechanism –we didn’t have time to do that this time.

Additionally the mosaic uses both MEF and MVVM Light (both of which we’ll be exploring in future posts). If you are nearby I will be speaking at the Pasco .NET Users Group on July 15th (2010) on the internals of the Hooters Mosaic.