Tech Blog

Jay's Technical blog

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