IgorShare Thoughts and Ideas

Consulting and Training

Hosted Prism (or how to host Prism in the old projects)

Posted by Igor Moochnick on 09/02/2008

Recently I’m playing a lot with Prism and Unity and was looking for a way to gradually bring Prism to the existing projects. If you have an existing GUI project and you’d like to start using Prism (without rewriting everything from scratch) stay with me – I’m going to show a way that worked for me.

In order to work with Prism you need a “root” window, or, how they call it, a “shell” window. But what if you already have a window? There is a solution to that.

First of all create a WPF user control that will become a “shell” window to Prism. The XAML rules for this control is not different from any other Prism “shell” window. Just the root element of it is a “UserControl” and not “Window”. Let’s call it “ShellContainer”.

Then you need to attach this control to your existing application. But first you need to tell the Bootstrapper about it, but you don’t need to activate it:

   1: protected override DependencyObject CreateShell()
   2: {
   3:     return Container.Resolve<ShellContainer>();
   4: }

If your project is Windows Forms-based, you’ll need to drop on your form an ElementHost control. This control is located on the toolbox in the WPF Interoperability group. This will allow you to host a WPF User control on you non-WPF form.

   1: // Get the ShellContainer from IOC
   2: IUnityContainer container = new UnityContainer();
   3: var shellElement = container.Resolve<myLib.ShellContainer>();
   4:
   5: // Attach the WPF control to the host
   6: hostControl.Child = shellElement;

If you have a WPF form you can add this WPF User Control either manually.

   1: // Get ShellContainer from IOC
   2: IUnityContainer container = new UnityContainer();
   3: var shellElement = container.Resolve<myLib.ShellContainer>();
   4:
   5: // Add the ShellContainer UserControl to the main window
   6: this.mainGrid.Children.Add(shellElement);

From this point on you are on a home run – just follow the rest of the Prism guidelines.

TIP: I found it convenient to put the ShellContainer with the Bootstrapper in the same class library with the rest of the configuration logic. This allows a clean separation of the infrastructure from the rest of the logic of the Prism modules.

Advertisements

4 Responses to “Hosted Prism (or how to host Prism in the old projects)”

  1. […] Hosted Prism (or how to host Prism in the old projects) […]

  2. Vishal said

    Hi,
    I read your article. It’s interesting.
    One question:
    How do you activate the ShellContainer in your approach and where do you call the run method of bootstrapper?

    Thanks & regards,
    Vishal.

  3. Anthony said

    Igor,

    I am having problems with hosting a WPF prism app in an element host and am desparate for help. Thought you might be able to help if you had a moment based on some of your blog posts.

    The PRISM app runs fine in silverlight and in a standalone WPF.
    The main Shell seems to setup fine in the elementHost on a WinForm however other views only load with the “RegisterViewWithRegion” and not the “Add,Activate” procedure. I need “Add,Activate” for scoping. However I beleive the problem is that I am loading my shell twice … not on purpose. I was just trying to follow your “Hosted Prism (or how to host Prism in the old projects)” and some other posts and basically I must call “Resolve” twice or so it seems. Your post has code snippets but it is not quite clear where they go or how they work. Seems like with your code there is no mention of when to instantiate the bootstrapper.

    If you have any sample code that shows this and dont mind shareing … I am all ears.

    Thanks in advance for your blog. It has been helpful in many ways.

  4. cloud host , cloud server , cloud vps , cloud computing, cloud hosting , joomla cloud hosting, wordpress cloud hosting , opencart cloud hosting , magento cloud hosting , vpn…

    […]Hosted Prism (or how to host Prism in the old projects) « IgorShare Weblog[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: