Question

I'm using Silverlight 5 with Prism and MEF.

I am trying to replace my shell at runtime by reading a XAML file, create an UIElement from it and replace the old shell's content with the new UIElement. I am using XamlReader.Load() for this.

This works, until I try to create Prism regions.

I can create a new Shell with just one prism region in it, but when I have two or more regions in the new shell, all I get is a blank screen in my browser, and no error messages.

Is there a way to debug this? And why is this happening?

Code:

Creating the UIElement and replacing the shell (in Shell.xaml.cs):

 DependencyObject rootObject = XamlReader.Load(XAMLFileString) as DependencyObject;
 UIElement customShell = rootObject as UIElement;
 this.Content = customShell;

This works (one region):

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:prism="http://www.codeplex.com/prism">

    <StackPanel>            
        <ContentControl  prism:RegionManager.RegionName="Region1"/>
    </StackPanel>

</UserControl>

This also works (two regular content controls):

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:prism="http://www.codeplex.com/prism">

    <StackPanel>
        <ContentControl Content="C1"/>
        <ContentControl Content="C2"/>
    </StackPanel>
</UserControl>

but this gives me a blank screen instead (two prism regions):

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:prism="http://www.codeplex.com/prism">

    <StackPanel>            
        <ContentControl  prism:RegionManager.RegionName="Region1"/>
        <ContentControl  prism:RegionManager.RegionName="Region2"/>
    </StackPanel>

</UserControl>
Was it helpful?

Solution

I figured out what the problem was: there was actually an exception, but it was silently handled in App.xaml.cs:

Microsoft.Practices.Prism.Regions.Behaviors.RegionCreationException: An exception occurred while creating a region with name 'Region1'. The exception was: System.ArgumentException: Region with the given name is already registered: 

The exception was thrown because I was trying to create a region with a name that already existed in my original Shell. After changing the region names, everything worked.

I still don't know why I got a white screen with two regions but not with one (both regions threw the same exception so one region also shouldn't have worked).

To replace a shell with another that has the same region names, the regions can be unregistered before replacing the shell:

_regionManager.Regions.Remove(regionname)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top