2012-08-14 135 views
1

我是WPF和Caliburn Micro的新手,需要关于如何组织我的应用的一些建议。Caliburn Micro:此应用的最佳方法

该应用程序是数据挖掘框架的配置编辑器。在应用程序中,将有一个主窗口,在左栏中有网格和树视图,在右栏中有不同的设置编辑器。显示在右侧的特定编辑器取决于在树视图中选择的项目。每个编辑器都由多个控件组成(在一个案例中最多可达200​​个)。

在编辑器之间切换时,我认为以前的编辑器应该隐藏/取消关闭,以提高切换速度。

我现在想知道的是什么是组织这个应用程序的最佳方式,以及如何在编辑器之间切换?

回答

1

假设您使用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... 
    } 
} 
相关问题