6 个 ViewModel 和 Messenger 之间的通信 == AntiPattern?

发布于 2024-09-27 00:56:34 字数 2580 浏览 5 评论 0原文

2 个 ViewModel 之间通信的常见方法是:MVVM-视图模型-视图模型通信< /a>

中介者模式或信使类。但是一个窗口中有 6 个 ViewModel 又如何呢?

  1. NewSchoolclassUserControl
  2. NewPupilUserControl
  3. SchoolclassListUserControl
  4. PupilListUserControl
  5. PupilsDetailUserControl
  6. AdministrationButtonBarUserControl (有执行命令的按钮)

所有这些都在一个窗口中。 “你”真的告诉我现在必须为这 6 个视图及其 Viewodels 设置一个 Messenger 吗?那太糟糕了......

一个窗口中有 6 个 UserControls,即使是大型企业应用程序在一个窗口中也没有更多的 UserControls,那么在这种情况下可接受的/最佳实践是什么?

我对拥有大型 mvvm 应用程序经验的人的意见感兴趣:)

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

更新:对于盲人来说;-)

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

这个 ViewModel 实际上是 DataGrid 的数据模板。期间就是行。每行都有一个称为文档的列。我有一个 periodListViewModel 1 : N DocumentListViewModel。

DocumentListViewModel 是使用包含 ListBox 的 UserControl 进行数据模板化的,下面是一些按钮添加/删除/保存/打开等...

DocumentListViewModel 具有在“LessonController”中执行的命令和操作委托,因此文档上的每个操作(如添加、删除等)都是如此。 .. 可以在 LessonController 中声明的 SelectedPeriodViewModel 上完成。

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

您需要更多代码吗?或者您对我的方法有何看法?我渴望学习,并且对每一位批评者感到高兴!

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

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
    (having buttons executing commands)

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 in one Window, not even a big enterprise app has more UserControls in a window, so whats an accepted/best practice in that case?

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

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.

UPDATE: for the blind meise ;-)

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

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.

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2024-10-04 00:56:34

如果你有 6 或 1000 个松散耦合的视图模型,它们应该相互通信,那么你应该使用信使/中介。它与用户控件完全无关。

如果你的视图模型相互引用,那么你就不需要信使,但它不再是松散耦合的:)

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

想象你有一个视图模型,它可以什么都不做,然后选择一个日期。这将是一个简单的松散耦合的虚拟机。您现在所能做的就是选择新日期后,通过信使发送消息。

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

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

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

现在,所有其他松散耦合的视图模型都可以注册到调解器和“SelectedDateChanged”消息,并在日期更改时执行所需的操作。
这可能不适合您的设计,但应该可以让您对信使模式有所了解。

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.

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

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)
     }
 }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文