Jatin
Jatin

Reputation: 4063

Caliburn Micro - ActivateItem using Container

I was going through the Caliburn Micro documenation here. Simultaneously, I was trying to put up some rough code for experiment. I am a little confused about how to activate item using a container and how to pass an object to the ViewModel that we are activating.

Lets consider a master/detail scenario. The master contains a list (say datagrid) and the details contain specific row from the master for update(say tab item inside tab control). In the documentation (for ease of understanding), I believe the detail ViewModel was directly instantiated using code like this

public class ShellViewModel : Conductor<IScreen>.Collection.OneActive {
int count = 1;

public void OpenTab() {
    ActivateItem(new TabViewModel {
        DisplayName = "Tab " + count++
    });
}
}

So, to apply the above fundamental concept in real world app, we need to instantiate the DetailViewModel (TabViewModel above) using container(say MEF). The challenge then is to know whether the particular DetailViewModel is already opened in the TAB Control. The immediate crude thing that came to my mind was maintaining a List of the Opened Tabs (DetailViewModels). But then we are again referencing DetailViewModel in the MasterViewModel defeating the purpose. Is there any options available to solve this issue.

The second thing that is troubling me is how to pass the Objects from MasterViewModel (Selected Detail Item) to the DetailViewModel. If we use the EventAggregator here then each of the opened DetailViewModels will receive the event which I am not sure how to handle.

If anyone can throw some light on the above two issues, I would be grateful

Update: The Master is Conductor like this

public class MainViewModel : Conductor<IScreen>.Collection.OneActive, IShell {
    ....
}

And the detail is defined like this

public class TabViewModel : Screen {
    ....
}

Both are in the same Window.

Upvotes: 1

Views: 3958

Answers (1)

devdigital
devdigital

Reputation: 34349

I'm not sure exactly what the issue is. In your conductor of many, you have an Items collection provided by Caliburn.Micro. When you come to display a detail view, you can check this collection for the existence of that detail view (using the primary key which you have from the master view).

If the item is already in the Items collection then just activate it (using the ActivateItem method). If the item isn't in the collection, then instantiate it (presumably using a factory if you're using MEF), and add it to the Items collection, and then activate it.

Upvotes: 1

Related Questions