My app is an IDE add-on, so the project is a wpf user control. I want to use Prism to refector it now. When I debug the app, it threw such an exception in InitializeComponent();
of the MainView
's constructor:
"ServiceLocationProvider must be set."
I also found a similar thread here: Strange exception in Prism application but there's no solution.
This is my bootstrapper class:
class Bootstrapper : UnityBootstrapper
{
protected override System.Windows.DependencyObject CreateShell()
{
return ServiceLocator.Current.GetInstance<MainView>();
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog catalog = (ModuleCatalog)this.ModuleCatalog;
catalog.AddModule(typeof(ModuleInit));
}
}
This is my ModuleInit class:
public class ModuleInit : IModule
{
IRegionManager regionManager;
IUnityContainer container;
public ModuleInit(IUnityContainer container, IRegionManager regionManager)
{
this.container = container;
this.regionManager = regionManager;
}
public void Initialize()
{
this.container.RegisterType<ViewModelA>(new ContainerControlledLifetimeManager());
this.container.RegisterType<MainViewModel>(new ContainerControlledLifetimeManager());
this.regionManager.RegisterViewWithRegion(Models.RegionNames.ARegion, () => this.container.Resolve<ViewModelA>());
}
}
Because this is a user control project, so there's no App.xaml and App.xaml.cs file. I can bootstrapper's Run method in MainView's constructor:
public MainView()
{
InitializeComponent();
Bootstrapper strapper = new Bootstrapper();
strapper.Run();
}
But the exception is thrown in the first line of the constructor. Anyone can help?
Furthermore, is there a way to use Region without modularity? My app only contains one project, it needn't modularity. If yes, where can I register the views, services and viewmodels?