代码之家  ›  专栏  ›  技术社区  ›  juFo

使用Caliburn.Micro与Ninject和WPF在视图模型之间绑定/传递数据

  •  2
  • juFo  · 技术社区  · 11 年前

    我有一个显示选项对话框的应用程序。 在选项对话框中,我显示了独角兽的列表。 当我选择独角兽时,我可以编辑或删除它。 当我想编辑独角兽时,它会在选项对话框上方显示另一个EditUnicorn对话框。 “编辑独角兽”对话框包含选项卡页,每个选项卡页都可以编辑独角兽的特定数据。

    Application 
    --> Options window showing unicorns (OptionsView)
    ----> edit unicorn dialog  (EditUnicornView)
    ------> tabpages with usercontrols inside the edit unicorn dialog to fill in specific data about unicorn. (tabpages: EditUnicornSkillsView, EditUnicornFriendsView, EditUnicornGeneralView, ...)
    

    我的GUI中的独角兽模型实际上更像是一个视图模型。。。

    public class Unicorn 
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Strength { get; set; }
        public Health HealthStatus { get; set; }
        public List<Unicorn> Friends { get; set; }
    }
    
    
    public class OptionsViewModel : PropertyChangedBase 
    {
            public ObservableCollection<Unicorn> Unicorns { get return MyData.Unicorns; }
    
            private Unicorn _SelectedUnicorn;
            public Unicorn SelectedUnicorn { 
                get { return _SelectedUnicorn; }
                set {
                    _SelectedUnicorn = value;
                    NotifyOfPropertyChange(() => CanAddUnicorn);
                    NotifyOfPropertyChange(() => CanEditUnicorn);
                    NotifyOfPropertyChange(() => CanDeleteUnicorn);
                }
            }
    
            public void EditUnicorn() {
                // Is this correct?
                WindowManager.ShowDialog(IoC.Get<EditUnicornViewModel(), SelectedUnicorn, null);
            }
    }
    
    public class EditUnicornViewModel : Screen 
    {
        // should it be like this? (or via the constructor or ...?)
        public Unicorn Unicorn { get; set; } 
    }
    

    EditUnicornView.xaml包含:

    <TabControl>
        <TabItem Header="General">
            <ContentControl x:Name="EditUnicornGeneralViewModel" />
        </TabItem>
        <TabItem Header="Skills">
            <ContentControl x:Name="EditUnicornSkillsViewModel" />
        </TabItem>
        <TabItem Header="Friends">
            <ContentControl x:Name="EditUnicornFriendsViewModel" />
        </TabItem>
    </TabControl>
    

    选项卡页中用户控件的视图模型:

    public class EditUnicornGeneralViewModel : PropertyChangedBase
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    
    public class EditUnicornSkillsViewModel : PropertyChangedBase
    {
        public string Strength { get; set; }
        public Health HealthStatus { get; set; }
    }
    
    public class EditUnicornFriendsViewModel : PropertyChangedBase 
    {
        public List<Unicorn> Friends { get; set; }
    }
    

    我在GUI应用程序中创建了一个Unicorn Model类,它实际上更像是一个视图模型, 我之所以创建这个,是因为选项卡页面中的每个用户控件都有一个特定的视图模型,只显示必要的数据。我不确定我这样做是否真的正确。

    现在的问题是,正如您所看到的,EditUnicornViewModel(几乎)是空的。。如何将选定的Unicorn传递到EditUnicornViewModel。 如何将一个视图模型的属性添加/注入/绑定/设置到另一个视图模式的另一个属性?(ninject+caliburn.micro) 然后同样的问题再次出现:如何在EditUnicornViewModel中的每个EditUnicorn(常规/技能/朋友)ViewModel中设置特定的Unicorn字段?

    编辑:

    我认为这不是一种正确的工作方式(然后我仍然不知道如何使用选项卡):

    public class OptionsViewModel : PropertyChangedBase 
    {
      // ...
    
      public void EditUnicorn() 
      {
         var vm = IoC.Get<EditUnicornViewModel>();
         vm.Unicorn = SelectedUnicorn;
         WindowManager.ShowDialog(vm, null, null);
    
      }
    }
    
    1 回复  |  直到 11 年前
        1
  •  1
  •   McDonnellDean    11 年前

    Caliburn.Micro配备了一个强大的EventAggregator,它提供了一种在系统中传递数据的干净方式,而不需要假设有人在监听。这将是您的最佳选择,因为这意味着N个选项卡可以监听和发送消息。看见 http://caliburnmicro.codeplex.com/wikipage?title=The%20Event%20Aggregator&referringTitle=Documentation

    编辑:

    我只想补充一点,好好阅读一下这些文档,Caliburn。Micro是基于构图的理念,你永远不应该打电话给国际奥委会。振作起来。基本上,你的应用程序应该这样组成堆栈

     Shell > Conductor > Conducted ViewModels
    

    看看回购中的样本,因为它们显示了很多很酷的组合特征。