Question

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 ElementHost. The following code is in the form:

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

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

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

    }

The BootStrapper handles regisration

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

The MyView.xaml is nothing more than a label at this point.

Shell.xaml is a UserControl that contains the following XAML:

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

The module code is minimal:

public class MyModule : IModule
{
    private readonly IRegionViewRegistry _regionViewRegistry;

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

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

I've been tracing deep into the Prism code trying to figure out why the View is never set into the region. Am I missing something basic?

Was it helpful?

Solution

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).

OTHER TIPS

@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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top