Reputation: 1051
I have created a small demo application using WPF Prism and I am using Mef.
Here is the Shell of the Application:
<Window ..........>
<Grid>
<ContentControl
prism:RegionManager.RegionName="{x:Static inf:RegionNames.ContentRegion}" />
</Grid>
</Window>
Here ContentRegion is just a static string defined in another class of infrastructure project.
Here is my Bootstrapper class:
public class Bootstrapper : MefBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.GetExportedValue<Shell>();
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
}
protected override void ConfigureAggregateCatalog()
{
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleMyModule).Assembly));
}
}
As you can see I have added my main executing project and its Infrastructure project to this Bootstrapper.
Now I have created a very simple Module called MyModule. It has a class called ModuleMyModule:
[ModuleExport(typeof(ModuleMyModule), InitializationMode = InitializationMode.WhenAvailable)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ModuleMyModule : IModule
{
IRegionManager _regionManager;
[ImportingConstructor]
public ModuleMyModule(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void Initialize()
{
_regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(MyView));
}
}
Now, I have a View called MyView in this application as follows:
<UserControl ............>
<Grid>
<CheckBox Content="Have you Checked it properly?"/>
</Grid>
</UserControl>
Upto this point my application works fine.
The problem starts now:
Now, I added a ViewModel to this project. So, now my view MyView looks like:
<UserControl ............>
<Grid>
<CheckBox IsChecked="{Binding IsProperlyChecked}" Content="Have you Checked it properly?"/>
</Grid>
</UserControl>
And Here is the .cs file of MyView:
[Export(typeof(MyView))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class MyView : UserControl, IView
{
[ImportingConstructor]
public MyView(IMyViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
}
public IViewModel ViewModel
{
get
{
return (IViewModel)DataContext;
}
set
{
DataContext = value;
}
}
}
Here is my ViewModel class:
[Export(typeof(MyViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MyViewModel : IMyViewModel, INotifyPropertyChanged
{
[ImportingConstructor]
public MyViewModel()
{
IsProperlyChecked = true;
}
private bool _IsProperlyChecked;
public bool IsProperlyChecked
{
get
{
return _IsProperlyChecked;
}
set
{
if (_IsProperlyChecked != value)
{
_IsProperlyChecked = value;
OnPropertyChanged("IsProperlyChecked");
}
}
}
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
IMyViewModel is an interface as shown below:
public interface IMyViewModel : IViewModel
{
bool IsProperlyChecked { get; set; }
}
Now, my project stops working:
I am getting an error:
An exception has occurred while trying to add a view to region 'ContentRegion'.
- The most likely causing exception was was: 'Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type MyView, key "" ---> Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type MyView, key ""
at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key)
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
--- End of inner exception stack trace ---
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType)
at Microsoft.Practices.Prism.Regions.RegionViewRegistry.CreateInstance(Type type)
at Microsoft.Practices.Prism.Regions.RegionViewRegistry.<>c__DisplayClass1.<RegisterViewWithRegion>b__0()
at Microsoft.Practices.Prism.Regions.Behaviors.AutoPopulateRegionBehavior.OnViewRegistered(Object sender, ViewRegisteredEventArgs e)'.
Why this exception is thrown??
I think I am doing something wrong. I am very new to MEF, I used Unity in past.
There I need to register ViewModel and its interface. But I don't know if its required in MEF. If required then How???
Demo Project :
https://drive.google.com/file/d/0Bw2XAE1EBI6rU3VsYjVyQmhFRFE/view?usp=sharing
Upvotes: 2
Views: 1935
Reputation: 6882
When using MEF a type passed as a parameter in ExportAttribute
should match the type expected by import(s).
Since MyView
constructor requires IMyViewModel
type:
[ImportingConstructor]
public MyView(IMyViewModel viewModel)
{
..
... try exporting MyViewModel class as IMyViewModel
[Export(typeof(IMyViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MyViewModel : IMyViewModel, INotifyPropertyChanged
{
...
Upvotes: 4