Model-View-Presenter和Modal对话框....如何? [英] Model-View-Presenter and Modal Dialog boxes.... How to?

查看:128
本文介绍了Model-View-Presenter和Modal对话框....如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WPF中实现MVP / M-V-VM,到目前为止我还是很幸运的。但是,我看不到这个模型如何支持实现Modal对话框。我从Crack.NET导出了我的工作( http://www.codeplex.com/cracknetproject )来了解这些东西如何工作。



我有一个ShellView视图(只是XAML),它有一个菜单。菜单绑定到ShellModelView中的一个命令,该命令表示EditPreferences。



ShellModelView实现了EditPreferences的ICommand,在这里我们要放置一个对话框,让用户编辑应用程序的首选项。



这里有几个问题:
1. ShellModelView没有引用ShellView来正确地将对话框加载到父对话框中。 ShellModelView是ShellView的DataContext,但我没有看到设置的反向引用。
2. ShellModelView不应该加载显式UI。那么这里有什么恰当的互动模式呢?
3.如何建立我的PreferencesDialog,以便在逻辑和视图之间正确分隔? PreferencesDialog本身需要一个Window,所以你可以在其上调用ShowDialog,但这意味着你需要一个Window参考(例如View)来实例化它。理想情况下,我可以在PreferencesDialog中对代码/验证进行单元测试,而无需实例化视图(也可以使用Mock视图)。

解决方案

p>也许这不是看待它的恰当方法,但这是我在WPF中使用MV-VM的方法。打开窗口和对话框或EditPreferences视图是UI特定的功能。如果我要重写整个UI,替换所有的视图,我可能会结合EditPreferences视图与另一个视图,因此不想打开它在另一个屏幕。如果这被绑定到ViewModel,那将很难解决。在这种特殊情况下,我将在ShellView中创建一个按钮或菜单项,创建一个新的EditPreferences视图,然后传入EditPreferencesViewModel,该模型可能来自我的ShellViewModel 或者也许我的EditPreferences视图实例化了ViewModel本身。



这是一个类似于SO的问题,基本上说同样的事情:MV-VM设计问题。来自ViewModel的呼叫视图


I am implementing MVP/M-V-VM in WPF and I'm having good luck with it so far. However, I don't see how this model supports implementing Modal dialog boxes. I've derived my work from Crack.NET (http://www.codeplex.com/cracknetproject) to learn how this stuff works.

I have a ShellView view (which is just XAML) that has a menu on it. The menu binds to a command in the ShellModelView that says "EditPreferences".

The ShellModelView implements the ICommand for EditPreferences and here we want to put up a dialog box to let the user edit preferences for the application.

Several problems here: 1. The ShellModelView doesn't have a reference to the ShellView to properly parent the dialog. The ShellModelView is the DataContext of the ShellView but I don't see a backreference that's setup. 2. The ShellModelView shouldn't be loading explicit UI anyway. So what's the proper interaction model here? 3. How do I build up my PreferencesDialog so that it's properly separated between logic and view as well? PreferencesDialog itself needs to be a Window so you can call ShowDialog on it, but that means you need a reference to the Window (e.g. View) in order to instantiate it. Ideally I should be able to unit test the code/validation within PreferencesDialog without instantiating the view (using a Mock view perhaps?).

解决方案

Perhaps this is not the appropriate way to look at it, but this is the approach I take with M-V-VM in WPF. Opening windows and dialog boxes or an "EditPreferences" view are UI specific functions. If I were to rewrite the your entire UI replacing all of the views, I may wind up combining the "EditPreferences" view with another view, and therefore never want to open it in another screen. If this were tied to the ViewModel, it would be difficult to get around. In this particular situation, I would have a button or menu item in my "ShellView" that creates a new instance of my "EditPreferences" view, and then passes in the "EditPreferences" ViewModel which may either come from a property in my "ShellViewModel", or perhaps my "EditPreferences" view instantiates the ViewModel itself.

Here is a similar question on SO that basically says the same thing: M-V-VM Design Question. Calling View from ViewModel

这篇关于Model-View-Presenter和Modal对话框....如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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