Activate does not show the view

Topics: Prism v2 - Silverlight 3
Sep 8, 2009 at 5:29 AM

I have simple prism application which has two regions: TopContent and MainContent. When the application starts it shows single button in the TopContent. Using the MVVM model the button's Command property is binded to ViewModel's command. When user clicks the button I would like to show something in the MainContent region. I have working and not working solution for this problem and I'm trying to understand why both of them are not working.

In my ViewModel I have the following code (executed when user clicks the button) which works:

 

this.regionmanager.RegisterViewWithRegion("MainContent",() => this.container.Resolve<TemplateEditorViewModel>().View);

If I put breakpoints on the TemplateEditorView (view which should be shown) and in the TemplateEditorViewModel then following happens:

 

1. TemplateEditorView constructor
2. TemplateEditorViewModel constructor
3. TemplateEditorView constructor
4. TemplateEditorViewModel constructor
5. TemplateEditorView loaded event raised
6. TemplateEditorViewModel catches TemplateEditorView's loaded event

Everything works just fine. I don't know why objects are created twice.

Now here is the code that does not work (the view is now shown):

TemplateEditorViewModel viewModel = this.container.Resolve<TemplateEditorViewModel>();
IRegion main_region = this.regionmanager.Regions["MainContent"];
main_region.Add(viewModel.View);
main_region.Activate(viewModel.View);

Again if I put breakpoints on the TemplateEditorView (view which should be shown) and in the TemplateEditorViewModel then following happens:

1. TemplateEditorView constructor
2. TemplateEditorViewModel constructor

Both objects are created just once and there is no loaded event and the view is not shown.

If I look at the quick start samples (ViewInjectionComposition in the EmployeesController's OnEmployeeSelected event) they use also the second way to show the view. I don't understand why it doesn't work for me.

TemplateEditorView is a standard UserControl and TemplateEditorViewModel's constructor takes following parameters:

public TemplateEditorViewModel(ITemplateEditorView view, IRegionManager regionManager, IUnityContainer container)

Sep 8, 2009 at 11:09 PM

Hi

Without knowing your entire code base, the code you provided seems OK, and it should work as you said (since it is done similarly in the QS).

One possible reason for this issue happening could be that you are creating the Shell twice. You can read more about a possible cause for this in this thread.

That could explain why with ViewDiscovery two views are being created (there are two regions with the same name), and why ViewInjection is not showing the view (it is showing the view in the instance of the Shell that is not visible).

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

 

Sep 9, 2009 at 10:21 AM

The link to the other thread (startup uri) helped alot and I found the problem.

As usual the problem is in my code and not in Prism at all.

In the Bootstrapper I had the following code:

        protected override System.Windows.DependencyObject CreateShell()
        {
            var shell = new Shell();
            Application.Current.RootVisual = new Shell();
            return shell;
        }

It is pretty obvious why I couldn't see the view. Since part of the application works fine I thought there must be something wrong in the code I'm using to display the view (region.Add/Activate).