如何在ElementHost中使用Prisim

时间:2009-09-01 16:17:13

标签: wpf prism cal cag

我是Prism的新手,我正在尝试在ElementHost中托管Prisim控件。我似乎错过了一些非常基本的东西。我有一个包含ElementHost的WinForm。以下代码的格式如下:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Bootstrapper bootstrapper = new Bootstrapper();
        bootstrapper.Run();

        var child = bootstrapper.Container.Resolve<Shell>();
        elementHost.Child = child;

    }

BootStrapper处理注册

public class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        Container.RegisterType<MyView>();
        var shell = Container.Resolve<Shell>();
        return shell;
    }

    protected override IModuleCatalog GetModuleCatalog()
    {
        ModuleCatalog catalog = new ModuleCatalog();
        catalog.AddModule(typeof(MyModule));
        return catalog;
    }
}

此时MyView.xaml只不过是一个标签。

Shell.xaml是一个包含以下XAML的UserControl:

<ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />

模块代码很小:

public class MyModule : IModule
{
    private readonly IRegionViewRegistry _regionViewRegistry;

    public MyModule(IRegionViewRegistry registry)
    {
        _regionViewRegistry = registry;   
    }

    public void Initialize()
    {
        _regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(MyView));
    }
}

我一直在深入研究Prism代码,试图找出为什么View从未进入该区域。我错过了一些基本的东西吗?

2 个答案:

答案 0 :(得分:4)

原因是Prism中的这段代码:

private static bool RegionManager::IsInDesignMode(DependencyObject element)
{
    // Due to a known issue in Cider, GetIsInDesignMode attached property value is not enough to know if it's in design mode.
    return DesignerProperties.GetIsInDesignMode(element) || Application.Current == null
        || Application.Current.GetType() == typeof(Application);
}

原因是对于非WPF应用程序,Application.Current为NULL!

解决方案:

  1. 创建一个将从System.Windows.Application继承的空类。 (姓名无所谓):
  2. 在进入插件时,执行以下代码:

    public class MyApp : System.Windows.Application
    {
    }
    
    if (System.Windows.Application.Current == null)
    {
        // create the Application object
        new MyApp();
    }
    

    就是这样 - 现在你有一个不是null的Application.Current,它不等于typeof(Application)。

答案 1 :(得分:0)

@Mark Lindell Above为我工作。我唯一需要改变的是下面的内容。

我的引导程序

 public  class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return this.Container.Resolve<Shell>();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();    
            if (System.Windows.Application.Current == null)
            {
                // create the Application object
                new HelloWorld.Myapp();
            }

            //App.Current.MainWindow = (Window)this.Shell;
            //App.Current.MainWindow.Show();
            //MainWindow = (Window)this.Shell;

        }     

        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();

            ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
            moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule));
        }

和我的表单类

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Create the ElementHost control for hosting the WPF UserControl
            ElementHost host = new ElementHost();
            host.Dock = DockStyle.Fill;

            Bootstrapper bootstrapper = new Bootstrapper();
            bootstrapper.Run(true);

            //var uc = bootstrapper.Container.Resolve<Shell>(); This line threw error

            //Create the WPF UserControl.          

                            HelloWorld.Shell uc = new HelloWorld.Shell();

            //Assign the WPF UserControl to the ElementHost control's Child property.
            host.Child = uc;

            //Add the ElementHost control to the form's collection of child controls.
            this.Controls.Add(host);
        }
    }   


        }

为了清楚起见,我在包含Shell的WPF PRISM应用程序中添加了以下类。

public class MyApp : System.Windows.Application
{
}
  

编辑:请注意,必须创建Load方法处理程序(表单)   右键单击表单,在属性窗口中,转到事件并加倍   clicl Load。复制和粘贴加载事件处理程序不起作用。