本文介绍了6个ViewModels与Messenger之间的通信== AntiPattern?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2个ViewModel之间进行通信的常用方法是:

A common approach for communication between 2 ViewModels is this: MVVM- View Model-View Model Communications

介体模式或Messenger类。但是在一个窗口中有6个ViewModel呢?

The Mediator pattern or a Messenger class. But what about 6 ViewModels in one Window?


  1. NewSchoolclassUserControl

  2. NewPupilUserControl

  3. SchoolclassListUserControl

  4. PupilListUserControl

  5. PupilsDetailUserControl

  6. AdministrationButtonBarUserControl
    (必须执行按钮命令)

  1. NewSchoolclassUserControl
  2. NewPupilUserControl
  3. SchoolclassListUserControl
  4. PupilListUserControl
  5. PupilsDetailUserControl
  6. AdministrationButtonBarUserControl(having buttons executing commands)

所有这些都在一个窗口中。现在,您真的告诉我,我必须为这6个视图及其Viewodels设置Messenger吗?这将是可怕的...

All this is in ONE Window. Do "you" really tell me now I have to setup a Messenger for those 6 Views and their Viewodels? That would be terrible...

在一个窗口中有6个UserControls,甚至没有一个大型企业应用程序在一个窗口中也有更多的UserControls,那么在那一个公认的/最佳实践是什么?

6 UserControls in one Window, not even a big enterprise app has more UserControls in a window, so whats an accepted/best practice in that case?

我会对在大型mvvm应用程序方面有经验的人感兴趣:)

I would be interested in someones opinion having experience with big mvvm applications :)

其中一些UserControl + ViewModels我想在应用程序的其他地方重用。因此,将所有内容整合到一个UserControl中并不是我真正想要的。

Some of those UserControl+ViewModels I would like to reuse at other places of my application. So put all in one UserControl is not that what I really want.

更新:盲目的;-)

private DateTime _selectedDate;
        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set
            {
                if (_selectedDate == value)
                    return;

                _selectedDate = value;
                this.RaisePropertyChanged("SelectedDate");


                ObservableCollection<Period> periods = _lessonplannerRepo.GetLessonDayByDate(SelectedDate);

                _periodListViewModel = new ObservableCollection<PeriodViewModel>();

                foreach (Period period in periods)
                {
                    PeriodViewModel periodViewModel = new PeriodViewModel(period);

                    foreach (DocumentListViewModel documentListViewModel in periodViewModel.DocumentViewModelList)
                    {
                        documentListViewModel.DeleteDocumentDelegate += new Action<List<Document>>(OnDeleteDocument);
                        documentListViewModel.AddDocumentDelegate += new Action(OnAddDocument);
                        documentListViewModel.OpenDocumentDelegate += new Action<Document>(OnOpenDocument);
                    }

                    _periodListViewModel.Add(periodViewModel);

                }
            }
        }

@blindmeise

@blindmeise

此ViewModel实际上是数据模板化到DataGrid的模板。时期就是行。每行都有一个称为文档的列。我有一个PeriodListViewModel 1:N DocumentListViewModel。

This ViewModel is datatemplated actually to a DataGrid. The Periods are the Rows. Each Row has a Column called Documents. I have a PeriodListViewModel 1 : N DocumentListViewModel.

DocumentListViewModel是使用UserControl进行数据模板化的,其中包含一个ListBox,并且在某些按钮下方有添加/删除/保存/打开等...

The DocumentListViewModel is datatemplated with a UserControl containing a ListBox and below some buttons add/del/save/open etc...

DocumentListViewModel具有在 LessonController中执行的命令和动作委托,因此对文档中的每个操作(如add,del等)都可以在声明的SelectedPeriodViewModel中进行LessonController。

A DocumentListViewModel has Commands and Action delegates executed in the "LessonController" so every action on a Document like add,del etc... can be done on the SelectedPeriodViewModel declared in the LessonController.

上面的代码只是在用户更改日期选择器中的日期时从数据库加载新数据。

The above code just loads new data from database when the user changes the date in the datepicker.

您需要更多代码,或者您对我的方法有何评价?我很想学习,也为每个批评家感到高兴!

Do you need more code or what do you say about my approach? I am eager to learn and I am glad about every critics!

推荐答案

如果您有6或1000个松耦合的ViewModel,它们应该进行通信彼此之间,则应该使用Messenger / Mediator。

if you have 6 or 1000 loosly coupled viewmodels which should communicate with each other, then you should use the messenger/mediator. it has nothing to do with usercontrols at all.

如果您的视图模型相互引用,那么您就不需要Messenger了,但是再也不用太松耦合了:)

if your viewmodels reference to each other then you have no need for a messenger, but its no more loosly coupled then :)

编辑:
真的很难说你能做些什么,因为我不知道你想用你的应用程序和appdesign实现什么:)通常,这取决于您如何为视图模型指定任务以及如何耦合这些视图模型。也许您应该从www上检查一些示例项目:)有很多mvvm实现,它们的变化很大,但是可以更好地理解mvvm模式(模式!不规则!!))

edit:its really hard to say what can you to better, cause i dont know what do you want to achieve with your app and your appdesign :) in general it depends on how you specify the tasks for your viewmodels and how do you want to couple these viewmodels. maybe you should check some sample projects from over the www :) there are a lot mvvm implementation which vary a lot but gives a better understanding on the mvvm pattern(pattern!! not rule!! ;))

想象您有一个视图模型,该模型不执行任何操作,然后选择一个日期。这将是一个简单的松散耦合的虚拟机。您现在所能做的就是选择一个新日期时,通过信使发送一条消息。

imagin you have a viewmodel which do nothing then select a date. this would be a simple loosly coupled vm. all you can do now is when a new date is selected, send a message through a messenger.

public DateTime SelectedDate
{
    get { return _selectedDate; }
    set
    {
         if (_selectedDate == value)
             return;

          _selectedDate = value;
          this.RaisePropertyChanged("SelectedDate");

          this.messenger.Notify("SelectedDateChanged", this.SelectedDate)
     }
 }

现在,所有其他松散耦合的视图模型都可以注册到调解器和 SelectedDateChanged消息中,并可以执行更改日期后想要做的事情。
,这可能不适合您的设计,但应该使您对Messenger模式有所了解。

now all other loosly coupled viewmodels could register to the mediator and the "SelectedDateChanged" message and do what the want to do when a date changed.this maybe does not fit in your design, but should give you an idea on the messenger pattern.

这篇关于6个ViewModels与Messenger之间的通信== AntiPattern?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:23