As I continue to build out a WP7 application using Caliburn Micro I ran into a odd error. When trying to inject the INavigationService I would get a ‘Missing Method’ exception as shown below:
When I first started to look at the error it did not make any sense. Everything appeared to be in order, but of course there had to be something wrong. lets take a look at what was wrong and how to resolve it.
When building out a WP7 app using Caliburn.Micro when you let the Visual Studio template the template will create a ‘framework’ folder for you. This framework folder has some killer code in it, but be careful because much of what is generated here also lives in the Caliburn.Micro namespace (in fact all this code may be duplicated, but I am too new to the framework to know for 100%).
The first thing I did when setting up my application is to add the following method to my PhoneContainer.cs class (this is based on a few example projects I have seen)
public void RegisterPhoneServices( bool treatViewAsLoaded = false )
RegisterInstance( typeof( Caliburn.Micro.INavigationService ), null, new FrameAdapter( _bootstrapper.RootFrame, treatViewAsLoaded ) );
RegisterInstance( typeof( Caliburn.Micro.IPhoneService ), null, new PhoneApplicationServiceAdapter( _bootstrapper.PhoneService ) );
RegisterSingleton( typeof( IWindowManager ), null, typeof( WindowManager ) );
RegisterSingleton( typeof( IEventAggregator ), null, typeof( EventAggregator ) );
The intent of the code above is to setup the IoC container with many of the default services I want to use from the framework. You will notice that in the first two I have prefixed the interfaces with Caliburn.Micro (which we my undoing as I will learn). The reason for this is that my project spans multiple dll’s and this was how I could get it to compile.
When I would try to run this code:
var instance1 = container.GetInstance( typeof( DashboardViewModel ), "DashboardViewModel" );
This when I would get the missing method exception from above. After about 10 minutes of looking at this code and trying a few different things I knew for a FACT that the error was due to the usage of the INavigationService interface, now just to understand what the root cause was.
As I went back to the location where I register the interface it downed on me. What is the namespace of the ‘FrameAdapter’ object? Of course it was NOT Caliburn.Micro…hum could this be the cause. Turns out it was. When I prefixed FrameAdapter to look like below
RegisterInstance( typeof( Caliburn.Micro.INavigationService ), null, new Caliburn.Micro.FrameAdapter( _bootstrapper.RootFrame, treatViewAsLoaded ) );
Everything now worked as expected. The moral of the story is this. If you are going to use this awesomeness which is Caliburn.Micro either use the libraries from the Caliburn.Micro.dll or DON’T. Do NOT mix apples and oranges.
Till next time,
P.S. I am sure I am really doing something wrong in general w/ Caliburn.Micro but hey that is how you learn right
05-09-2011 7:40 AM