How to use Prism within an ElementHost

后端 未结 2 864
耶瑟儿~
耶瑟儿~ 2020-12-20 06:24

I\'m new to Prism and I am attempting to host a Prisim control within an ElementHost. I seem to be missing something very basic. I have a single WinForm that contains an El

2条回答
  •  囚心锁ツ
    2020-12-20 06:38

    @Mark Lindell Above worked for me. The only things I had to change are below.

    My bootstrapper

     public  class Bootstrapper : UnityBootstrapper
        {
            protected override DependencyObject CreateShell()
            {
                return this.Container.Resolve();
            }
    
            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));
            }
    

    and my form class

    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(); 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);
            }
        }   
    
    
            }
    

    And just to be clear, I added below class in the WPF PRISM application containing Shell.

    public class MyApp : System.Windows.Application
    {
    }
    

    Edit: Note that the Load method handler (of form) has to be created by rightclicking form, In the properties window, go to events and double clicl Load. Copying and pasting load event handler doesn't work.

提交回复
热议问题