Activate a different view in a XBAP application

Topics: Prism v2 - WPF 3.5
Mar 10, 2009 at 3:58 PM

I am not able to replace the initial view I have in a XBAP application
I've used HelloWorld.Desktop sample.
Here are the changes that I made:
  • I added another module, which is the same as HelloWorldModule, I simply changed the text it displays.
  • I added the module in the Bootstrapper GetModuleCatalog. My new module is added second after the original one
  • In HelloWorld.Desktop App.xaml I added StartupUri="Window1.xaml" to the attributes of Application
  • In Window.xaml I changed the Window element to a Page. I also changed the ItemsControl to ContentControl
  • In Window.xaml.cs I changed the parent class to be Page (and not Window)
  • I added a way to switch the views
  • private

    void TextBlock_MouseRightButtonUp(object sender, MouseButtonEventArgs e)




    IRegion mainRegion = this.regionManager.Regions["MainRegion"];



    object selectorView = mainRegion.Views.ElementAt(1);




  • I removed the line shell.Show(); in the Bootstrapper.cs

So, if the shell is a Window and shell.Show exists than switching the views works. However, the xbap scenario that I need it doesn't.

I clearly need some help to make it work.



Mar 10, 2009 at 10:16 PM
Edited Mar 17, 2009 at 5:35 PM

Hi Mihael,


[Updated] The workaroundsuggested here might not work in all scenarios. To address this problem pleasetake a look at the following post below or at the blog<o:p>   Migrating Composite Application Guidance (Prism-v2) to XBAP.</o:p>

We were able to reproduce the error you mentioned and we found a possible workaround. Take into account that this is just a workaround and it's not a recommended  best practice, since the CALs source code is being modified. We tested it with your scenario but it might not work on some other situations. To implement the workaround follow these steps:

1.       In the Microsoft.Practices.Composite.Presentation.Regions.ContentControlRegionAdapter class declare a private instance variable of ContentControl type named RegionTarget:

                               private ContentControl RegionTarget;

2.       Inside the Adapt method set the private variable RegionTarget to the regionTarget parameter:

this.RegionTarget = regionTarget;

3.       In the delagate for the CollectionChanged event, replace the line:

regionTarget.Content = region.ActiveViews.FirstOrDefault();

for the following one:

this.RegionTarget.Content = region.ActiveViews.FirstOrDefault();


The Adapt method's outcome should be similar to the following:

this.RegionTarget = regionTarget;           

            region.ActiveViews.CollectionChanged += delegate


                this.RegionTarget.Content = region.ActiveViews.FirstOrDefault();          



Matias Bonaventura
Mar 11, 2009 at 3:31 PM
It worked in the real application as well.
Thanks Matias.
Mar 12, 2009 at 4:37 PM
Edited Mar 12, 2009 at 4:40 PM

Thanks, I'm glad it worked out.

After testing and researching a bit more on this issue I realized that even though the previous solution works for the scenario you have, the core problem was not being addressed.

Although modifying the ContentControlRegionAdapter solves the issue, the real problem is that you are creating 2 instances of the Shell page (one is being created on the bootstrapper and another is being created by the WPF framework when you specify the starupUri).


To overcome this, there is a better solution that is less intrusive (you don't need tochange CAL's code) and that will prevent other errors you might have in the future. You might find useful the following post in my blog that explains this with more detail:

·        Migrating Composite Application Guidance (Prism-v2) to XBAP


Hope it helps!

Matias Bonaventura