使用MVVM在WPF中创建新窗口的最佳方法(The best approach to create new window in WPF using MVVM)

系统教程 行业动态 更新时间:2024-06-14 17:03:52
使用MVVM在WPF中创建新窗口的最佳方法(The best approach to create new window in WPF using MVVM)

在邻居帖子中: ViewModel应该如何关闭窗体? 我已经发布了我的愿景如何关闭使用MVVM的窗口。 现在我有一个问题:如何打开它们。

我有一个主窗口(主视图)。 如果用户点击“显示”按钮,则应显示“演示”窗口(模态对话框)。 使用MVVM模式创建和打开窗口的最佳方式是什么? 我看到两种一般方法:

第一个(可能是最简单的)。 事件处理程序“ShowButton_Click”应该在主窗口后面的代码中实现,方式如下:

private void ModifyButton_Click(object sender, RoutedEventArgs e) { ShowWindow wnd = new ShowWindow(anyKindOfData); bool? res = wnd.ShowDialog(); if (res != null && res.Value) { // ... store changes if neecssary } } 如果我们“显示”按钮状态应该被改变(启用/禁用),我们将需要添加将管理按钮状态的逻辑; 源代码非常类似于“旧式”WinForms和MFC源 - 我不知道这是好还是坏,请指教。 我错过了什么?

另一种方法:

在MainWindowViewModel中,我们将实现将返回该命令的ICommand接口的“ShowCommand”属性。 柯曼依次:

将提出“ShowDialogEvent”; 将管理按钮状态。

这种方法将更适合于MVVM,但需要额外的编码:ViewModel类不能“显示对话框”,因此MainWindowViewModel将仅提供“ShowDialogEvent”,MainWindowView将需要在其MainWindow_Loaded方法中添加事件处理程序,如下所示:

((MainWindowViewModel)DataContext).ShowDialogEvent += ShowDialog;

(ShowDialog - 类似于'ModifyButton_Click'方法。)

所以我的问题是:1.你看到其他方法吗? 你认为列出的是好还是坏? (为什么?)

欢迎任何其他想法。

谢谢。

In the neighbour post: How should the ViewModel close the form? I've posted my vision how to close windows with MVVM usage. And now I have a question: how to open them.

I have a main window (main view). If user clicks on the "Show" button then "Demo" window (modal dialog) should be displayed. What is a preferable way to create and open windows using MVVM pattern? I see two general approaches:

The 1st one (probably the simplest). Event handler "ShowButton_Click" should be implemented in the code behind of the main window in way like this:

private void ModifyButton_Click(object sender, RoutedEventArgs e) { ShowWindow wnd = new ShowWindow(anyKindOfData); bool? res = wnd.ShowDialog(); if (res != null && res.Value) { // ... store changes if neecssary } } If we "Show" button state should be changed (enabled/disabled) we will need to add logic that will manage button state; The source code is very similar to "old-style" WinForms and MFC sources - I not sure if this is good or bad, please advise. Something else that I've missed?

Another approach:

In the MainWindowViewModel we will implement "ShowCommand" property that will return ICommand interface of the command. Comman in turn:

will raise "ShowDialogEvent"; will manage button state.

This approach will be more suitable for the MVVM but will require additional coding: ViewModel class can't "show dialog" so MainWindowViewModel will only raise "ShowDialogEvent", the MainWindowView we will need to add event handler in its MainWindow_Loaded method, something like this:

((MainWindowViewModel)DataContext).ShowDialogEvent += ShowDialog;

(ShowDialog - similar to the 'ModifyButton_Click' method.)

So my questions are: 1. Do you see any other approach? 2. Do you think one of the listed is good or bad? (why?)

Any other thoughts are welcome.

Thanks.

最满意答案

我也在考虑这个问题。 如果您在项目中使用Unity作为“容器”,或者依赖注入任何操作,我都有一个想法。 我想通常你会覆盖App.OnStartup()并创建你的模型,查看模型并查看那里,并给出每个相应的参考。 使用Unity,您可以向容器提供对模型的引用,然后使用该容器“解析”该视图。 Unity容器注入您的视图模型,因此您不会直接实例化。 一旦您的视图解决,您就可以在其上调用Show() 。

在我观看的示例视频中,Unity容器在OnStartup创建为一个局部变量。 如果您在App类中将其创建为公共静态readonly属性怎么办? 然后,您可以在主视图模型中使用它来创建新窗口,自动注入新视图需要的任何资源。 像App.Container.Resolve<MyChildView>().ShowDialog(); 。

我想你可能会在你的测试中模拟对Unity容器的调用结果。 或者,也许您可​​以在App类中编写ShowMyChildView()方法,这些方法基本上只是在上面描述的。 可能很容易模拟对App.ShowMyChildView()的调用,因为它只会返回一个bool? 呃

那么这可能并不比使用new MyChildView()更好,但是我有一个想法。 我以为我会分享 =)

I was thinking about this issue recently too. Here's an idea I had if you use Unity in your project as a 'container' or whatever for dependency injection. I guess normally you'd override App.OnStartup() and create your model, view model, and view there, and give each the appropriate references. Using Unity, you give the container a reference to the model, then use the container to 'resolve' the view. The Unity container injects your view model, so you never directly instantiate it. Once your view is resolved, you call Show() on it.

In an example video I watched, the Unity container was created as a local variable in OnStartup. What if you created it as a public static readonly property in your App class? You could then use it in your main view model to create your new windows, automatically injecting whatever resources the new view needs. Something like App.Container.Resolve<MyChildView>().ShowDialog();.

I suppose you could somehow mock the result of that call to the Unity container in your tests. Alternatively, perhaps you could write methods like ShowMyChildView() in the App class, which basically just does what I described above. It might be easy to mock a call to App.ShowMyChildView() since it would just return a bool?, eh?

Well, that might not really be any better than just using new MyChildView(), but it's a little idea I had. I thought I'd share it. =)

更多推荐

本文发布于:2023-04-24 12:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/2e8789f4488406bcc582d4c0dc7f65fe.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:建新   中创   窗口   方法   MVVM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!