Prism 4.0:重写InitializeShell()方法
我一直在浏览创建 Prism 应用程序和设置 Shell 的文档,似乎分为 2 个方法,CreateShell() 和 InitializeShell()
对于 CreateShell 我只是有:
protected override DependencyObject CreateShell()
{
return ServiceLocator.Current.GetInstance<Shell>();
}
文档说 IntializeShell() 中需要代码方法以确保它已准备好显示。下面给出了一个例子:
protected override void InitializeShell()
{
Application.Current.MainWindow = (Window)this.Shell;
Application.Current.MainWindow.Show();
}
但是我注意到,如果我省略第一行并只调用 Show() 方法,它似乎可以工作(MainWindow 似乎已经分配了 Shell)。你能告诉我为什么会出现这种情况吗?为什么我们仍然需要在这里显式设置 MainWindow 属性?
另外,由于我没有专门将 Shell 注册到容器内的接口,那么它如何能够在 CreateShell() 中解析 Shell 呢?
I've been going through the documentation for creating Prism applications and setting up the Shell seems to be split into 2 methods, CreateShell() and InitializeShell()
For CreateShell I simply have:
protected override DependencyObject CreateShell()
{
return ServiceLocator.Current.GetInstance<Shell>();
}
The documentation says that code is needed in the IntializeShell() method to ensure it is ready to be displayed. The following is given as an example:
protected override void InitializeShell()
{
Application.Current.MainWindow = (Window)this.Shell;
Application.Current.MainWindow.Show();
}
I have noticed however that if I omit the first line and just call the Show() method it seems to work (MainWindow already appears to have Shell assigned to it). Can you tell me why this is the case, and why we still need to explicity set the MainWindow property here?
Also as I did not specifically register Shell to an interface within the container, how is it able to resolve Shell in CreateShell()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题 1:为什么仅调用 Show() 似乎有效,为什么 Application.Current.MainWindow 似乎已填充?
您应该在此处检查一些事项。在典型的 WPF 应用程序中,可以在 App.xaml 中指定主窗口的类型。如果已指定,WPF 将为您实例化其中之一。这是不可取的,因为 WPF 不会使用您的容器来实例化您的 shell,并且不会解析任何依赖项。
当您在 InitializeShell 中运行第一行代码时,您将用手动实例化的对象替换 WPF 实例化的 Shell 对象。
我查看了 MEF 和 Unity 引导程序的代码,没有看到 MainWindow 被设置的任何地方,但我不知道您是否可能自定义了基本引导程序,所以这是需要寻找的其他内容。
Show() 之所以有效,是因为您只是显示实例化的窗口,而 WPF 实例化的窗口不会显示。这是我的理论,但如果没有看到你的代码,就很难确定。
问题 2:Unity 如何解析尚未注册的内容?
Unity 始终可以解析具体类型,无论是否注册。它无法解析尚未映射到具体类型的非具体类。这就是为什么
Resolve
可以工作,但Resolve
不能工作,除非您注册了类型。Question 1: Why does just calling Show() seem to work and why is Application.Current.MainWindow seem to be populated?
There are a few things you should check here. In a typical WPF application, the type for the main window can be specified in the App.xaml. If it is specified, WPF will instantiate one of those for you. This is not desirable because WPF won't use your container to instantiate your shell and any dependencies won't be resolved.
When you run that first line of code in InitializeShell, you'd be replacing the WPF-instantiated Shell object with the one you manually instantiated.
I looked at the code for the MEF and Unity bootstrappers and I don't see anywhere that MainWindow is being set, but I don't know if you might have customized the base bootstrappers, so that's something else to look for.
Show() works because you are simply showing the window you instantiated and the WPF-instantiated one isn't shown. This is my theory, but without seeing your code, it'd be tough to say for sure.
Question 2: How can Unity resolve something that hasn't been registered?
Unity can always resolve a concrete type, regardless of registration. It cannot resolve non-concrete classes that haven't been mapped to a concrete type. This is why
Resolve<Shell>
works, butResolve<IMyInterface>
doesn't unless you register a type.