1
votes

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?

1
Why are you using the service locator to find an instance of the shell? Why not have unity resolve on it?Gayot Fow
@GayotFow using this piece of code has the same issue:protected override System.Windows.DependencyObject CreateShell() { return this.Container.TryResolve<MainView>(); }Allen4Tech
That's the question to solve. If unity cannot do it, then something's seriously wrong. Using the service manager because the container throws an exception is just hiding the problem.Gayot Fow
I see the problem now. You are trying to run a boot strapper from within the constructor. Initialize component is being called twice because main view has never been constructed.Gayot Fow
@GayotFow Where should I call the run method? There's no App.xaml and App.xaml.cs file in my projectAllen4Tech

1 Answers

0
votes

Somewhere, for example in your bootstrapper before init code, you have to define your IoC container. This is how it's done in ViewModelLocator by default:

ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);