C# 获取对Shell窗口当前实例的引用

C# 获取对Shell窗口当前实例的引用,c#,wpf,prism,C#,Wpf,Prism,我正在尝试显示模式对话框,我需要引用当前Shell窗口: public class OpenPopupWindowAction : TriggerAction<FrameworkElement> { protected override void Invoke(object parameter) { var popup = new ChildWindow(); //(ChildWindow)ServiceLocator.Current.GetInsta

我正在尝试显示模式对话框,我需要引用当前Shell窗口:

public class OpenPopupWindowAction : TriggerAction<FrameworkElement>
{
    protected override void Invoke(object parameter)
    {
        var popup = new ChildWindow(); //(ChildWindow)ServiceLocator.Current.GetInstance<IPopupDialogWindow>();
        popup.Owner =  PlacementTarget ?? (Window)ServiceLocator.Current.GetInstance<IShell>();
这是Bootstrapper的代码

public class Bootstrapper : UnityBootstrapper
{
    protected override System.Windows.DependencyObject CreateShell()
    {
        Container.RegisterInstance<IShell>(new Shell());
        return Container.Resolve<Shell>();

您将容器设置错误

这告诉Unity在请求
IShell
时返回
Shell
的特定实例:

Container.RegisterInstance<IShell>(new Shell());
因此,当您稍后从容器解析
IShell
时,将返回一个根本没有使用过的shell窗口,并且其窗口句柄尚未创建

改为这样做:

protected override System.Windows.DependencyObject CreateShell()
{
    var shell = new Shell();
    Container.RegisterInstance<IShell>(shell);
    return shell;
}
protectedoverride System.Windows.DependencyObject CreateShell()受保护的覆盖
{
var shell=新shell();
容器.寄存器状态(外壳);
返回壳;
}

谢谢,这很好用。您是否有其他建议,如何以更合适的方式实现这一点?我无法在子窗口中使用Shell,因为Shell所在的项目已经引用了我的子窗口所在的项目。解决方法是从子和Shell项目引用的基础结构项目IShell。@user927827:完全取决于上下文。与基础设施项目的安排听起来很合理。
Container.RegisterInstance<IShell>(new Shell());
return Container.Resolve<Shell>();
protected override System.Windows.DependencyObject CreateShell()
{
    var shell = new Shell();
    Container.RegisterInstance<IShell>(shell);
    return shell;
}