0

I have problem of thinking ideal solution for creating and showing window in WPF MVVM application. Some part of application needs to show some window with some data. I create VM, set its properties, create View, assign its VM (in constructor), then display window. This is done using class that I named ViewController and this class have methods with parameters for every window in my application. I think there can be better solution than this, but not overengineered.

Ondřej
  • 1,645
  • 1
  • 18
  • 29
  • 1
    I usually create a manager that manages window (maintains `DataContext`, operates window actions such as WindowOpen event, WindowClose event, etc). To set new `View` in specific window I just call method `windowManager.OpenView(some_optional_parameters)`. Above manager, there is `ApplicationController`, which manages all windows managers and allows to keep multiple windows in one time. – Fka Jun 19 '15 at 09:13
  • Looks like you have one window and stuff various content (Views) inside. In my case, one view = one window with various controls. – Ondřej Jun 19 '15 at 09:20
  • Yes and no. My approach allows me to have muliple windows and each window can (but it's not mandatory) have multiple views inside. It's 3 layer presentation architecture - `ApplicationManager` -> `WindowManager` -> `ViewModel`. – Fka Jun 19 '15 at 09:27
  • Anyway, it is same solution as Daniel Slater is suggesting, so I can't use it. – Ondřej Jun 19 '15 at 10:29
  • See [WPF MVVM navigate views](http://stackoverflow.com/questions/19654295/wpf-mvvm-navigate-views/19654812#19654812) question. – Sheridan Jun 19 '15 at 13:02
  • Maybe you should add some example code showing what your perfect api call would look like? – Daniel Slater Jun 19 '15 at 13:04

2 Answers2

0

The normal solution is you have a class that wraps and instantiates a View ViewModel pair. This is often called screen. it would look something like this.

public class Screen<TView> where TView : Window
{
     public Screen(TView view, object viewModel){
         //store view and viewModel props
         //display view
         //set viewModel as DataContext of view
     }
}

This is a very rough example, there are lots of ways you can do it.

Daniel Slater
  • 4,123
  • 4
  • 28
  • 39
  • Every VM have different properties to set so I can not use something generic. And I don't want to end with Screen class for every V/VM combination. – Ondřej Jun 19 '15 at 09:15
  • This is no argument against the suggested solution. You could use reflection etc. – Beachwalker Jun 19 '15 at 11:06
  • You only have 1 screen class you then just pass in the constructed view and viewModel. Setting up the ViewModel is handled by setting up the view model however you want that done. – Daniel Slater Jun 19 '15 at 13:03
  • What is reason to have generic class that only put VM into view constructor and call Show()? I can do that manually where instances of view and VM are created. I will not use reflection, its pointless. – Ondřej Jun 20 '15 at 17:18
0

In the last I created implementation of IWindowManager, which have methods for showing required windows and these methods have parameters if needed. Methods create view model, set its properties and inject it to window. Only drawback of this solution is when new window is needed, new method must be added to interface and implementation of WindowManager.

Ondřej
  • 1,645
  • 1
  • 18
  • 29