WPF: Window management (opening, closing, etc.) In MVVM?

I read about it in a bunch of places. Most people link to these two links:

I do not understand any of them. I start when it comes to MVVM. Some people mention controllers when it comes to handling windows in MVVM. What is it and how are they implemented? By order, MVVM consists of a model, viewmodel and view - where do the controllers come in?

If someone could provide a sample of the following use case, that would be awesome (for all those people who are just starting to work with this, like me):

  • Prerequisite: A window opens.
  • User clicks a button.
  • A new window opens, and some data is transferred to this window, i.e. some string.
  • The new window is closed (or the button is pressed), and some data is transferred to the first window.
  • Past data has changed something in the window.
+5
source share
2 answers

The relationship of the ViewModel with the ViewModel is usually handled by the implementation of the event aggregator pattern.

MVVM Light uses a class Messenger, Prism has a different implementation, but basically this is one way to send messages between View Models without communication.

There are several examples and Articles describing use. I would suggest looking at him.

As for the controller elements in WPF, I don't know.

Regarding the example:

- Windows WindowsViewModel. , Button.

- . .

- .

- . ViewModel, ViewModel , .

- , , , , :

 bool? ShowDialogImpl<TViewModel>(Action<TViewModel> setup) where TViewModel : ViewModel
        {
            return (bool?)DispatcherHelper.UIDispatcher.Invoke(
                (Func<bool?>)(() =>
                              {
                                  var viewModel = viewModelFactory.Get<TViewModel>();
                                  viewModel.ViewService = this;
                                  setup(viewModel);
                                  var window = new Window
                                               {
                                                   Owner = this,
                                                   SizeToContent = SizeToContent.WidthAndHeight,
                                                   WindowStartupLocation = WindowStartupLocation.CenterOwner,
                                                   Content = ViewFactory.CreateView<TViewModel>(),
                                                   DataContext = viewModel,
                                                   WindowStyle = WindowStyle.ToolWindow,
                                                   ShowInTaskbar = false
                                               };
                                  window.SetBinding(TitleProperty, new Binding("Title"));
                                  openDialogs.Push(window);
                                  window.Activated += (sender, args) => window.SizeToContent = SizeToContent.Manual;
                                  var result = window.ShowDialog();
                                  openDialogs.Pop();
                                  viewModelFactory.Release(viewModel);
                                  return result;
                              }));
        }

: . factory . .

Windows - Grid, Dialog - . Inf the Windows :

messenger.Register<EntityUpdated<FooClass>>(this, message => UpdateItem(message.Entity));

:

messenger.Send(new EntityUpdated<FooClass>(subject));

, , - "", .

, :)

0

, (.. , ), viewmodel viewmodel , , , , - .

0

All Articles