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

查看:34
本文介绍了使用 MVVM 在 WPF 中创建新窗口的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有一个主窗口(主视图).如果用户单击显示"按钮,则应显示演示"窗口(模式对话框).使用 MVVM 模式创建和打开窗口的首选方法是什么?我看到两种通用方法:

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:

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

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
            }
        }

  1. 如果我们应该更改(启用/禁用)显示"按钮状态,我们将需要添加管理按钮状态的逻辑;
  2. 源代码与旧式"WinForms 和 MFC 源代码非常相似 - 我不确定这是好是坏,请指教.
  3. 还有什么我错过的吗?

另一种方法:

在 MainWindowViewModel 中,我们将实现ShowCommand"属性,该属性将返回命令的 ICommand 接口.依次命令:

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

  • 将引发ShowDialogEvent";
  • 将管理按钮状态.

这种方法更适合 MVVM,但需要额外的编码:ViewModel 类不能显示对话框",所以 MainWindowViewModel 只会引发ShowDialogEvent",我们需要在 MainWindowView 的 MainWindow_Loaded 方法中添加事件处理程序,像这样:

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 - 类似于ModifyButton_Click"方法.)

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

所以我的问题是:1. 你还有其他方法吗?2. 您认为其中一项是好是坏?(为什么?)

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.

谢谢.

推荐答案

我最近也在考虑这个问题.如果您在项目中使用 Unity 作为容器"或任何用于依赖项注入的内容,我有一个想法.我想通常你会覆盖 App.OnStartup() 并创建你的模型、视图模型和视图,然后给每个适当的引用.使用 Unity,您可以为容器提供对模型的引用,然后使用容器来解析"视图.Unity 容器会注入您的视图模型,因此您永远不会直接实例化它.一旦您的视图被解析,您就可以对其调用 Show().

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.

在我观看的示例视频中,Unity 容器是在 OnStartup 中创建为局部变量的.如果您在 App 类中将其创建为公共静态只读属性会怎样?然后你可以在你的主视图模型中使用它来创建你的新窗口,自动注入新视图需要的任何资源.类似于 App.Container.Resolve().ShowDialog();.

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

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

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?

嗯,这可能并不比使用 new MyChildView() 更好,但这是我的一个小想法.我以为我会分享它.=)

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. =)

这篇关于使用 MVVM 在 WPF 中创建新窗口的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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