.NET Framework Developer Center >
.NET Development Forums
>
Managed Extensibility Framework
>
Beginner MEF and MVVM questions
Beginner MEF and MVVM questions
- So I am trying to implement MEF with my WPF MVVM project (nice mouthful there!). I have been reading the documents on the http://mef.codeplex.com/ site, and have some questions.
So first, the only example I was able to find using MEF and MVVM:
http://consultingblogs.emc.com/davidwynne/archive/2009/05/22/wpf-model-view-viewmodel-mvvm-mef-and-other-acronyms.aspx
And they really go to town, using MEF to actually manage their viewmodels and views. Not sure I want to go that far yet, although maybe I should. But what I DO want to do is pass messages between ViewModels. Or, even better, I see that you can inject properties using MEF.
Does this mean if I have an ActiveView property in one view model that returns a ViewModelBase, I can access this property from another viewmodel? I tried (in my MainView):
[Export("ActiveView")]
public ViewModelBase ActiveView { get _activeView; set {...} }
And in another viewmodel:
[Import("ActiveView")]
public ViewModelBase ActiveView { get; set; }
Everything compiles but in the imported view model, ActiveView is null.
Which brings me to the next question, I think this is failing because I did not compose these correctly. How do I do that? The example from the MEF site says 'skip this step if you are using ASP.NET or WPF'. Skip what step, the whole thing or just the beginning?
I think Im missing how to setup this thing.
Also, is there any issue trying to access the property directly like this? Or is it better to just send string messages around? I thought it would be cool to try and send a property, or even better objects around for reuse between ViewModels but I havent gotten that far yet.
Thanks!
PS The more I read the docs the more I think I dont really understand this at all :P
Answers
- If you're going to use MEF with WPF, I recommend taking a close look at the "Meflook" sample that ships with MEF PR7.
This is a pretty good example of how to use MEF with WPF, and how to get things to tie together nicely.
Specifically, take a look at ISelectionService/SelectionService, and how that gets injected at runtime. Basically, in their WPF setup, everything's created via MEF's initial compose method, so you don't ever have to compose objects yourself.
Other implementations (like the blog you listed) compose parts at runtime, as well. (It's creating a new CompositionBatch and "composing" the view to create the viewmodel, and inject services at construction.)
------------
In your case, you'll need to Compose "this" in your constructor to get the Import constraints "filled in" with ActiveView elements.
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer bysaxisa Friday, October 30, 2009 5:20 PM
All Replies
- If you're going to use MEF with WPF, I recommend taking a close look at the "Meflook" sample that ships with MEF PR7.
This is a pretty good example of how to use MEF with WPF, and how to get things to tie together nicely.
Specifically, take a look at ISelectionService/SelectionService, and how that gets injected at runtime. Basically, in their WPF setup, everything's created via MEF's initial compose method, so you don't ever have to compose objects yourself.
Other implementations (like the blog you listed) compose parts at runtime, as well. (It's creating a new CompositionBatch and "composing" the view to create the viewmodel, and inject services at construction.)
------------
In your case, you'll need to Compose "this" in your constructor to get the Import constraints "filled in" with ActiveView elements.
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer bysaxisa Friday, October 30, 2009 5:20 PM


