【问题标题】:How to initializeShell in XBAP/Prism?如何在 XBAP/Prism 中初始化 Shell?
【发布时间】:2014-02-22 12:46:21
【问题描述】:

在 WPF/PRISM 中,当您创建 shell 时,它将分配给App.Current.MainWindow。但是在浏览器中的 WPF (XBAP) 中,该行:

App.Current.MainWindow = (Window)Shell;

失败并显示错误消息:

无法将“XBAPClient.Page1”类型的对象转换为“System.Windows.Window”类型。

protected override DependencyObject CreateShell()
{            
    return this.Container.GetExportedValue<Page1>();
}

protected override void InitializeShell()
{
    base.InitializeShell();
    App.Current.MainWindow = (Window)Shell;
    App.Current.MainWindow.Show();
}

如何加载外壳?

【问题讨论】:

  • 如果您使用 XBAP,您将使用 Page 而不是 Window。所以你不能将 Shell 转换为 Window。有关更多信息,请查看此线程:compositewpf.codeplex.com/discussions/…
  • 更新:我已关注第一条评论。说明很好。然而仍然没有运气。调查它为什么不运行。但至少它的建筑。谢谢
  • @kmatyaszek 不幸的是,CreateShell 先被调用,然后Page1.xaml.cs 才能设置Bootstrapper.ShellPage = this;因此它返回一个始终为空的 shell。你有什么建议吗?
  • 我已经找到解决方案,请检查我的答案。

标签: c# wpf mvvm prism xbap


【解决方案1】:

在 XBAP 中,Application.Current.MainWindowMS.Internal.AppModel.RootBrowserWindow

您应该只将您的页面分配给这样的窗口内容:

...
protected override void InitializeShell()
{
    base.InitializeShell();    
    Application.Current.MainWindow.Content = Shell;
}
...

【讨论】:

  • 你是明星!现在它确实有效。你是怎么想出来的? +1
  • 我刚刚调试了代码并试图解决这个问题 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-17
相关资源
最近更新 更多