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




daniel levite said
any more tips on using Ms Office as a shell?
igormoochnick said
Actually I’m working on a magazine article on this whole topic. Do you have a specific question on your mind?
Alan Huffman said
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.
igormoochnick said
Glad it was helpful
Mark said
I would also be very interested in seeing how you go about CAB hosting Prism?
igormoochnick said
You should not have any problems hosting Prism as soon as you initialise the RegionManager correctly and teach it where your main container resides. As well don’t forget to initialise the bootloader (or all the services needed to bootstrap Prism)