wpf Prism 应用程序中的奇怪异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22169996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strange exception in Prism application
提问by ProfK
I am trying to get a Prism application to start up, and am getting a very strange error:
我正在尝试启动 Prism 应用程序,但遇到一个非常奇怪的错误:
InvalidOperationException: ServiceLocationProvider must be set.
InvalidOperationException:必须设置 ServiceLocationProvider。
I am using MainWindowin the main (module host) application as the region for a single main shell, that has it's own regions. That way I can swap out main window layouts if needed.
我MainWindow在主(模块主机)应用程序中使用作为单个主 shell 的区域,它有自己的区域。这样我就可以在需要时换出主窗口布局。
I get the error on the InitializeComponent();call, the only line of code in the constructor of MainWindow. Google and Bing both return zero results for that exact phrase.
我在InitializeComponent();调用时遇到错误,这是MainWindow. Google 和 Bing 都为该确切短语返回零结果。
The XAML element in MainWindowis:
中的 XAML 元素MainWindow是:
<ContentControl regions:RegionManager.RegionName="MainShellRegion" />
Do I have to implement some interface or something on MainWindowto solve this? I am completely stumped.
我是否必须实现一些接口或其他东西MainWindow来解决这个问题?我完全被难住了。
回答by Jon
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 ConfigureServiceLocatormethod.
您需要为您的 Prism 应用程序设置依赖注入,否则它将无法运行。这应该从bootstrapper内部的ConfigureServiceLocator方法内部完成。
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.
对上述内容进行一点扩展,Prism 是连线的,因此无论何时需要访问应用程序组件,它都不会直接初始化该组件(它如何知道要使用哪个实现以及如何初始化它?)而是委派这项工作到服务定位器。
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).
服务定位器是一个组件,其职责是满足另一个组件对一个组件的请求,允许两个组件解耦。作为开发人员,您有责任实例化和配置服务定位器并使其可用于 Prism;这是在应用程序启动期间完成的(因此在引导程序内部)。
回答by Daniel Rose
As Jon wrote, Prism needs the ServiceLocator to be set. This is done in the bootstrapper and should happen in the abstract ConfigureServiceLocatormethod. The MefBootstrapperor UnityBootstrapperhave an implementation for that method where the service locator is set based on the composition container (which itself is set in ConfigureContainer).
正如 Jon 所写,Prism 需要设置 ServiceLocator。这是在引导程序中完成的,应该在抽象ConfigureServiceLocator方法中发生。的MefBootstrapper或UnityBootstrapper具有用于在服务定位器是基于该组合物的容器(其本身是在设置于设置方法的实现ConfigureContainer)。
These methods (and many more) are all called as part of the Runmethod on the bootstrapper.
这些方法(以及更多方法)都作为Run引导程序方法的一部分被调用。
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.
其他一切都应该在引导程序上的适当方法中完成。

