I'm trying to create a simple modular MVVM application with MEF. I have a ViewModel class and a UserControl as the View. I connect the two through a DataTemplate, like this:
<DataTemplate DataType="{x:Type local:MyViewModel}"> <local:MyView /> </DataTemplate> In the View, I define the ViewModel as a StaticResource, to make binding simple:
<UserControl.Resources> <local:MyViewModel x:Key="ViewModel" /> </UserControl.Resources> Then I bind like this:
<Grid DataContext="{StaticResource ResourceKey=ViewModel}"> <TextBlock Text="{Binding Text}" /> </Grid> This all works as intended without MEF. However, as I am aiming for modularity, I use MEF to discover my ViewModel classes. I have an Export attribute on my ViewModel class:
[Export(typeof(MyViewModel))] public class MyViewModel { // ... } and I use MEF to dynamically load the ViewModel into my shell in App.xaml.cs:
private void Application_Startup(object sender, StartupEventArgs e) { var shell = new MainWindow(); var catalog = new AssemblyCatalog(this.GetType().Assembly); var container = new CompositionContainer(catalog); shell.Contents.ViewModel = container.GetExportedValues<MyViewModel>().First(); shell.Show(); } Now, at this point, MEF creates an instance of my ViewModel when it loads the vm, and my View creates another instance when it declares the vm as a resource. (This is easily checked by setting a breakpoint in the constructor.)
The question is, how should I pass the instance created by MEF to my resource declaration? Can I declare that specific instance as resource?
DropBox link with full code: https://www.dropbox.com/sh/pbdl029d26sx7gl/AAA6po50dLjbJSoNPBhCyWZ3a?dl=0