none
How to instantiate PresentationModels correctly? (Composite WPF) RRS feed

  • Question

  • I am struggling with a Composite WPF application. I am using view injection and the structure of my project is very similar to this example: http://www.codeproject.com/KB/WPF/CompositeWpfApp.aspx

    My project has three tabs (DataSource, Graphs, Specifications). By default the DataSource tab is selected and the “DataSourceView” is displayed in the MainRegion.  On that view is a “PopulateData” button. When clicked, an Access database is queried, data sets are created, the “GraphsView” is shown in the “MainRegion”, and I should see my charts populated with data.

    However, this doesn’t happen. I see no data. To see data I must run the app, click the Graphs tab, click back on the DataSource tab, then hit “PopulateData”. I have to do this b/c hitting the Graphs tab is the only way the “GraphsPresentationModel” gets instantiated.  In its constructor is the “DataSourcePopulatedEvent” subscription:

    this .eventAggregator.GetEvent<DataSourcePopulatedEvent >().Subscribe(this .PopulateChartData, ThreadOption .UIThread, true );


    How can I instantiate the “GraphsPresentationModel” and subscribe to this event in code so I don’t have to click the Graphs tab, then come back to the DataSource tab?

    Sorry that was a bit long winded. If you need any clarification or have further questions please ask. Thanks in advance.

     

     

    Tuesday, June 8, 2010 2:32 PM

Answers

  • One thing I have done in the past is used a controller class to handle all of the view switching. In this class you can keep private versions of all of your views and then do something like the following when you need to switch

     

    if (view == null)
    {

        view = Container.Resolve<MyView>();
        RegionManager.RegisterViewWithRegion("MainRegion", () => view);
    }

    RegionManager.Regions["MainRegion"].Activate(view);

    • Marked as answer by Linda Liu Monday, June 14, 2010 3:45 AM
    Tuesday, June 8, 2010 5:33 PM

All replies

  • Make sure you do a registerType on all of your views on the container when the app is loaded.
    Tuesday, June 8, 2010 4:34 PM
  • I am doing that. Doing a RegisterType does not get you into that type's constructor. In my GraphsModule I do this:

    this.container.RegisterType<IPresentationModel, GraphsPresentationModel>(ModuleNames.GraphsModule, new ContainerControlledLifetimeManager());

    That doesn't get me into the GraphsPresentationModel constructor where this is:

    this.eventAggregator.GetEvent<DataSourcePopulatedEvent>().Subscribe(this.PopulateChartData, ThreadOption.UIThread, true);

    Tuesday, June 8, 2010 5:11 PM
  • One thing I have done in the past is used a controller class to handle all of the view switching. In this class you can keep private versions of all of your views and then do something like the following when you need to switch

     

    if (view == null)
    {

        view = Container.Resolve<MyView>();
        RegionManager.RegisterViewWithRegion("MainRegion", () => view);
    }

    RegionManager.Regions["MainRegion"].Activate(view);

    • Marked as answer by Linda Liu Monday, June 14, 2010 3:45 AM
    Tuesday, June 8, 2010 5:33 PM