WPF Prism 将用户控件设置为 Shell

WPF Prism set usercontrol as Shell

我是 WPF Prism 的新手,我看过很多关于 Prism 的在线文章,我发现所有示例代码都使用 windows 应用程序作为 shell,所以我有一个问题,可以我让 UserControl 成为 shell?如果不是,为什么?

shell 应该是 window,因为 WPF 应用程序始终具有顶级 window(除非作为 XBAP 托管在浏览器中),但您可以设置window 的 Content 到引导程序 InitializeShell() 方法中的用户控件:

protected override DependencyObject CreateShell()
{
    return Container.Resolve<MainWindow>();
}

protected override void InitializeShell()
{
    Application.Current.MainWindow.Content = new UserControl();
    Application.Current.MainWindow.Show();
}

UserControl 必须托管在 window 或页面中。它不是顶级控件。

  1. Window可以单独打开;而 UserControl 不能。这就是为什么 top-level window 必须是 Window.
  2. UserControl 也可以包含区域。这允许您使用区域将一个 UserControl 嵌套在另一个 UserControl 中。