How to use Prism within an ElementHost

后端 未结 2 865
耶瑟儿~
耶瑟儿~ 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<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));
            }
    

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

    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.

    0 讨论(0)
  • 2020-12-20 06:39

    The reason is this code in 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);
    }
    

    The reason is that for the non-WPF application the Application.Current is NULL!

    The solution:

    1. Create an empty class that will inherit from System.Windows.Application. (Name doesn’t matter):

    At the point of entry to a plug-in execute the following code:

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

    This is it – now you have an Application.Current that is not null and it’s not equal to typeof(Application).

    0 讨论(0)
提交回复
热议问题