简体   繁体   English

如何使用MVVM处理打开和关闭新Windows?

[英]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? 使用MVVM和WPF,处理打开和关闭新窗口和对话框的好方法是什么? Opening and closing should be driven by the ViewModel right? 打开和关闭应该由ViewModel驱动吗? But the ViewModel should not know about the view ... 但ViewModel不应该知道视图......

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. 例如,如果我想在单独的窗口中编辑记录,我有一个接口IEditingProvider <TViewModel>,我可以在其他地方实现,然后将接口引用传递给我的ViewModel的构造函数。 The EditingProvider might just do something like this: EditingProvider可能会做这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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