Strange exception in Prism application

吃可爱长大的小学妹 提交于 2019-12-05 16:11:46

You need to set up dependency injection for your Prism application, otherwise it won't be able to run. This should be done from inside your bootstrapper, inside the ConfigureServiceLocator method.

To expand a bit on the above, Prism is wired so that whenever it needs access to an application component it does not initialize the component directly (how would it know which implementation to use and how to initialize it?) but rather it delegates this job to a service locator.

The service locator is a component whose responsibility is to fulfill requests for one component made by another, allowing the two components to be decoupled. It is your responsibility as the developer to instantiate and configure the service locator and make it available to Prism; this is done during application startup (hence inside the bootstrapper).

As Jon wrote, Prism needs the ServiceLocator to be set. This is done in the bootstrapper and should happen in the abstract ConfigureServiceLocator method. The MefBootstrapper or UnityBootstrapper have an implementation for that method where the service locator is set based on the composition container (which itself is set in ConfigureContainer).

These methods (and many more) are all called as part of the Run method on the bootstrapper.

So my guess is that you are not calling the bootstrapper correctly. You code should look something like this:

public class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        var bootstrapper = new MyBootstrapper();
        bootstrapper.Run();
    }
}

Everything else should be done in the appropriate methods on the bootstrapper.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!