Using a controller with lob master/detail

Topics: Prism v4 - WPF 4
Apr 7, 2011 at 5:31 PM

On my master view model I am publishing an event with the contents that I want to pass to the detail.

 

ViewSettings storeViewSettings = new ViewSettings();
storeViewSettings.ViewName = ViewNames.CurrencyDetailView;
storeViewSettings.RegionName = RegionNames.SecondaryRegion;
storeViewSettings.CurrentItem = viewData.CurrentItem;

this.eventAggregator.GetEvent<ModuleChangeEvent>().Publish(storeViewSettings);

 

 

A control is subscribing to the event and activates the correct view.

 

private void DisplayView(ViewSettings viewSettings)
{
    this.ViewSettings = viewSettings;

    string viewName = viewSettings.ViewName;

    // Default to the main region if no region was specified.
    if (string.IsNullOrWhiteSpace(viewSettings.RegionName))
    {
        viewSettings.RegionName = RegionNames.MainRegion;
    }

    // Check to see if the region exists.
    if (this.regionManager.Regions.ContainsRegionWithName(viewSettings.RegionName))
    {
        IRegion region = this.regionManager.Regions[viewSettings.RegionName];

        object currentView = region.GetView(viewName);

        // If the view already exists in the region remove it.
        if (currentView != null)
        {
            region.Remove(currentView);
        }

        try
        {
            // Get a new instance of the view.
            currentView = ServiceLocator.Current.GetInstance<IView>(viewName);
        }
        catch (ActivationException activationException)
        { 
                logger.Log(activationException.ToString(), Category.Exception, Priority.High);
                return;
        }

        // Add the view to the region.
        region.Add(currentView, viewName);

        // Activate the view.
        region.Activate(currentView);
    }
}

 

 

My values are injected with the constructor and everything works.

[ImportingConstructor]
public CurrencyDetailViewModel(IGeneralController generalController)
{
    this.generalController = generalController;

    this.currency = this.generalController.ViewSettings.CurrentItem as Currency;
}

When I send a second value from the master view model, the detail view model's constructor is not called and my data is not changed..

What do I need to do set my values every time or have the constructor injected every time?

I have tried to follow TransactionInfo from the StockTrader example but I still cannot seem to figure it out.

 

Apr 7, 2011 at 7:22 PM

Hi,

Based on my understanding of your scenario, you need to decorate your CurrencyDetailViewModel class with the PartCreationPolicy attribute as NonShared. By doing this, a new instance of the exported view model will be created each time it is resolved. For example in your code it would look like this:

[Export(typeof(ICurrencyDetailViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class CurrencyDetailViewModel : ICurrencyDetailViewModel
{
    [ImportingConstructor]
    public CurrencyDetailViewModel(IGeneralController generalController)
    {
       this.generalController = generalController;
 
       this.currency = this.generalController.ViewSettings.CurrentItem as Currency;
    }
}

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 
Apr 7, 2011 at 9:11 PM

Miguel Bronzovic,

I went back and checked to make sure that the ViewModel's PartCreationPolicy was set to NonShared, and it was.

The problem turned out to be that the PartCreationPolicy on the View was set to Shared. Doh!

Thanks for quick response.