简体   繁体   中英

How do I handle opening and closing new Windows with MVVM?

With MVVM and WPF what would be a good/straightforward way to handle opening and closing new windows and dialogs? Opening and closing should be driven by the ViewModel right? But the ViewModel should not know about the view ...

I usually use interfaces for this. For example, if i want to edit a record in a separate window, i have an interface IEditingProvider<TViewModel>, which i can implement somewhere else and then pass an interface reference to the constructor of my ViewModel. The EditingProvider might just do something like this:

class MyRecordEditingProvider: IEditingProvider<MyRecordViewModel>
{
    // Implementation of generic interface method
    public void Edit(MyRecordViewModel model) {
        EditWindow edit = new EditWindow(); 
        edit.DataContext = model;
        edit.ShowDialog();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM