Caliburn.Micro指挥;我想念的是什么? [英] Caliburn.Micro Conductor; What Am I Missing?

查看:125
本文介绍了Caliburn.Micro指挥;我想念的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们,



我是Caliburn.Micro的新手,但不是MVVM或C#的新手,我真的,真的不想问这在StackOverflow上结束了。我已经成为这里的会员/潜伏者超过10年,我知道这里的人群会比StackOverflow人群更有帮助。



指挥.OneActive / .AllActive 有一个集合,您可以在其中放置要激活的对象;我明白了。从我所看到的,主要的方法是使用ActivateItem()方法,该方法将对象作为参数,将其添加到集合并激活,或者根据文档,如果它已经存在于集合中,它被激活了。或者,您可以使用Items成员直接添加到集合中。



根据某些标准定位和激活您无法轻松搜索的集合有什么用处你需要的对象?我正在尝试在内容控件中的视图模型之间设置一些简单的导航;没有什么棘手的。在视图中,我有一个contentControl绑定到Conductor的 ActiveItem 属性。在一个单独的视图/视图模型对中,我有一组按钮,用于触发下面类的Handle成员中处理的事件,我需要使用它来告诉Conductor要切换到哪个项目。请注意,所有构造函数参数都是通过 SimpleContainer 注入的。 NavigationVM中有按钮。



但问题出在这里:ActivateItem()将一个对象作为参数,这意味着你必须要有一个实例来启动它,Items集合中没有任何东西可以让你找到你需要的项目,而不是IndexOf(),它返回一个int。



我是否必须迭代项目集合,以找到我正在寻找的项目?没有什么比FindOne或其他任何东西......如何在没有它的情况下找到我想切换到的现有对象?这里重要的是,Items集合中的每个视图模型都需要在不活动时保持其状态,所以我不想关闭它们并再次重新创建它们,我需要将它们保存在集合中并将它们切换到这是一些代码:



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!

推荐答案

没关系; System.Linq扩展方法......



这很好用:

Nevermind; System.Linq extension method...

This works nicely:
Items.Add( RecordVM );
Items.Add( LoginVM );
.
.
.
ActivateItem( Items.First( vm => vm.GetType().Name == "LoginViewModel" ) );


这篇关于Caliburn.Micro指挥;我想念的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆