Hi folks,
I'm new to Caliburn.Micro, but not new to MVVM or C#, and I really, really don't want to ask this over at StackOverflow. I've been a member/lurker here for better than 10 years, and I know the crowd here will be more helpful than the StackOverflow crowd.
Conductor.OneActive / .AllActive have a collection into which you can place objects to activate; I get that. From what I can see, the main way of doing that is by using the ActivateItem() method, which takes an object as argument, which gets added to the collection and activated, or according to the documentation, if it already exists in the collection, it's activated. Alternatively, you can add to the collection directly using the Items member.
What good is a collection that you can't easily search based on some criteria to locate and activate the object you need? I'm trying to set up some simple navigation between viewmodels in a content control; nothing tricky. In the view, I have a contentcontrol bound to the
ActiveItem property of the Conductor. In a separate view/viewmodel pair, I have a set of buttons that fire events handled in the Handle member of the class below, which I need to use to tell the Conductor which item to switch to. Note that all of the constructor parameters are injected via
SimpleContainer. NavigationVM has the buttons in it.
But here's the problem: ActivateItem() takes an object as parameter, which means you have to have an instance of it to start with, and nothing on the Items collection allows you to find the item you need, other than IndexOf(), which returns an int.
Do I have to iterate over the Items collection to find the item I'm looking for? There's nothing like a FindOne or whatever... How do I locate the existing object I want to switch to without already having it?? The important thing here is that each of the viewmodels in the Items collection need to maintain their state when not active, so I don't want to close them and new them up again, I need to keep them in the collection and switch them in and out.
Here's some code:
public class ShellViewModel : Conductor<object>.Collection.OneActive, IShell, IHandle<NavigationMessage>
{
private readonly IEventAggregator _events;
public NavigationViewModel NavigationVM
{
get;
private set;
}
public ShellViewModel( INavigation NavigationVM, ILogin LoginVM, IRecords RecordVM, IEventAggregator events )
{
this.NavigationVM = NavigationVM as NavigationViewModel;
_events = events;
_events.Subscribe( this );
Items.Add( RecordVM );
ActivateItem( LoginVM );
}
public void Handle( NavigationMessage msg )
{
MessageBox.Show( string.Format("ViewID = {0}", msg.ViewID ) );
}
}
}
Many thanks in advance!