Opening new window from ViewModel

Derrick Moeller

How should I be opening new windows? I'm currently doing the following.

EventArgs:

public class GenericViewRequestedEventArgs : EventArgs
{
    public GenericViewModel ViewModel { get; private set; }

    public GenericViewRequestedEventArgs(GenericViewModel viewModel)
    {
        ViewModel = viewModel;
    }
}

ViewModel:

public class MainWindowViewModel : ViewModelBase
{
    private RelayCommand _viewSpecificCommand;

    public ICommand ViewSpecificCommand
    {
        get
        {
            if (_viewSpecificCommand == null)
                _viewSpecificCommand = new RelayCommand(x => viewSpecific());

            return _viewSpecificCommand;
        }
    }

    public EventHandler<GenericViewRequestedEventArgs> GenericViewRequested;

    private void RaiseGenericViewRequested(GenericViewModel viewModel)
    {
        var handler = GenericViewRequested;
        if (handler != null)
            handler(this, new GenericViewRequestedEventArgs(viewModel));
    }

    private void viewSpecific()
    {
        RaiseGenericViewRequested(_specificViewModel);
    }
}

View:

public partial class MainWindow : Window
{
    private void OnGenericViewRequested(object sender, GenericViewRequestedEventArgs e)
    {
        GenericWindow window = new GenericWindow(e.ViewModel);
        window.ShowDialog();
    }
}

This does work, but it seems like a lot of code and I end up with code behind in my view any ways.

  • What's the logic behind sending the command to the viewmodel at all?
  • Is it just to optionally use the predicate(if so why not bind to Enabled) and possibly avoid exposing additional viewmodels as properties?
  • Should I be attaching simple event handlers in the XAML(e.g. Click="btnViewSpecific_Click")?
Ken Hung

Yes, there are a lot of additional codes for MVVM. Building a command that independent of Views is usually for unit testing, such that the command and ViewModel can be unit tested without involving UI components.

However, if the "command" is just opening a window, it is not worth to create a command, and unit test the command to see if the GenericViewRequested is really fired(you can even check if the correct _specificViewModel is returned). The codes are far more complicated and just little value is added. Just open the window in View's button click event handler and it is fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Opening a New Window from Unity (Firefox)

From Dev

WPF opening a new window from an event

From Dev

Opening a new window using javascript with information from previous window

From Dev

Prevent Bootstrap modal window from opening in new tab / window

From Dev

Close actual Window and open a new Window from ViewModel

From Dev

Window not opening in a new tab

From Dev

opening a link in a new window

From Dev

Opening new window in wicket

From Dev

Opening New Window in Jframe

From Dev

How to disable chrome from opening up "new window" and "tabs"?

From Dev

opening another Android app in a 'new window' from Air

From Dev

Prevent exe from opening a new command prompt window

From Dev

Returning a file path from webservice and opening it in a new window

From Dev

Links are opening in a new tab/window by default from my web application

From Dev

Stop node-debug from opening a new browser window

From Dev

Prevent Ubuntu from opening a new Firefox window on every click

From Dev

Kde, opening a new terminal window to ssh from bash script

From Dev

Opening a new terminal window in C

From Dev

HTML "mailto:" not opening in new window

From Dev

Opening new window with button in OpenERP

From Dev

Click on link not opening in new window

From Dev

opening new window in PHP not working

From Dev

GUI, JComboBox and opening a new window

From Dev

Opening an SWF file in a new window

From Dev

opening new window and loading css and js from parent window, also share session

From Dev

Tweet button opening new window and new tab

From Dev

Prevent Revit window from opening

From Dev

Opening Modal Window from Link

From Dev

opening modal window from iFrame into parent window

Related Related

HotTag

Archive