如何处理使用MVVM打开和关闭新的Windows? [英] How do I handle opening and closing new Windows with MVVM?

查看:347
本文介绍了如何处理使用MVVM打开和关闭新的Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVVM和WPF,处理打开和关闭新窗口和对话框的方法是什么?开放和关闭应该由ViewModel驱动吗?但是ViewModel不应该知道这个视图...

解决方案

我通常使用接口。
例如,如果我想在单独的窗口中编辑记录,我有一个接口IEditingProvider< TViewModel>,我可以实现其他地方,然后传递一个接口引用到我的ViewModel的构造函数。 EditingProvider可能只是这样做:

  class MyRecordEditingProvider:IEditingProvider< MyRecordViewModel> 
{
//实现通用接口方法
public void Edit(MyRecordViewModel model){
EditWindow edit = new EditWindow();
edit.DataContext = model;
edit.ShowDialog();
}
}


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();
    }
}

这篇关于如何处理使用MVVM打开和关闭新的Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆