IgorShare Weblog

Practical Engineering

Prism (V2) RegionManager fails to recognize non-WPF applications

Posted by igormoochnick on 01/05/2009

In the recent change of Prism (V2), the way how the Region Manager detects that it’s running in a Design Mode or not, has been changed. This has ultimately broke all my Prism plug-ins that were hosted by non-WPF applications. One of the examples of hosts for such a plug-in, is an Office AddIn.

It took me about 6 hours to track down this problem and, to my amazement, I’ve found the place where it broke:

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. 1. Create an empty class that will inherit from System.Windows.Applicatoin. (Name doesn’t matter):
    public class MyApp : System.Windows.Application 	{ 	}
    

     

  2. At the point of entry to a plug-in execute the following code:
    
    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).

If, by any chance, you’d like to merge your application resources – do the following:

 // merge in your application resources
System.Windows.Application.Current.Resources.MergedDictionaries.Add(
    System.Windows.Application.LoadComponent(
        new Uri("MyLibrary;component/Resources/MyResourceDictionary.xaml",
        UriKind.Relative)) as ResourceDictionary);

For more information check DR.WPF’s article (http://www.drwpf.com/blog/Home/tabid/36/EntryID/10/Default.aspx) that beautifully explains the reasons and workarounds for the problem, described above.

4 Responses to “Prism (V2) RegionManager fails to recognize non-WPF applications”

  1. daniel levite said

    any more tips on using Ms Office as a shell?

  2. Thanks! Just spent 2 days on this… Hosting Prism in CAB so that we can slowly migrate from CAB to Prism (CAB/Prism/WPF/Winform interop)

    Fun.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>