假设您使用ViewModel的第一种方法,您是否检出了ISceens和Screen实现?目前的Caliburn.Micro下载包含一个带有屏幕协调器实现的演示项目(SimpleMDI)
您可以使用主视图模型中的Activate/Deactivate功能来处理'属性'视图之间的切换。
主视图模型应该来自校准提供的实现Conductor<T>.Collection.OneActive
,其中T应该是IScreen。这使您的主视图模型一次只能激活一个屏幕。
基本上,如果您将'selected item'绑定到主视图模型中的属性,则可以观察属性已更改的通知(使用NotifyOfPropertyChange),然后使用某种例程来决定切换到哪个视图。
Caliburn将保留视图缓存(在您的Conductor上使用GetChildren),以便您可以在它们之间切换 - 保持性能。
我使用类似这样的动态实例化基于数据库和可用库的控件(请注意,我的示例有点令人困惑,因为CurrentView实际上是一种自定义类型,并不是一个视图 - 它只是一个数据库对象其中描述了已被选中的控件..我应该改变它!)
public MainPageViewModel : Caliburn.Micro.Conductor<IScreen>.Collection.OneActive
{
#region Property Changed handler
public override void NotifyOfPropertyChange(string propertyName)
{
base.NotifyOfPropertyChange(propertyName);
// A property changed, update the views if it's the CurrentView property
UpdateViews(propertyName);
}
#endregion
private void UpdateViews(string propertyName)
{
// If the selected item in my list changes, it's bound to CurrentView and contains
// the fully qualified typename of the ViewModel that the items 'screen' should use
if (propertyName == "CurrentView")
{
// If the selected item changes we need to load the control or retrieve it from the
// control cache, making sure to update the cache and the active view dictionary
try
{
var controlType = Type.GetType(CurrentView.QualifiedTypeName, true);
// Check to see if the view is already loaded
var view = GetChildren().FirstOrDefault(x => x.GetType() == controlType);
// If it is, just activate it
if (view != null)
{
ActivateItem(view);
}
else
{
// Otherwise it's not loaded, load it and activate it
view = (IScreen)Activator.CreateInstance(controlType);
ActivateItem(view);
}
// etc...
}
}