Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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!
Posted
Updated 7-Dec-15 9:46am
v3
Comments
Richard MacCutchan 8-Dec-15 4:37am    
ddoutel 8-Dec-15 15:12pm    
Because somewhere in the documentation, I read that the author preferred that we use StackOverflow. Not being fond of StackOverflow, I figured I'd ask here.

1 solution

Nevermind; System.Linq extension method...

This works nicely:
C#
Items.Add( RecordVM );
Items.Add( LoginVM );
.
.
.
ActivateItem( Items.First( vm => vm.GetType().Name == "LoginViewModel" ) );
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900