最好的方法来使用MVVM WPF中创建新窗口最好的、方法来、建新、中创

2023-09-02 21:43:06 作者:骨子里的酷

在邻居后:http://stackoverflow.com/questions/501886/wpf-mvvm-newbie-how-should-the-viewmodel-close-the-form/2100824#2100824 我已为我的视力如何关闭窗户MVVM使用。现在我有一个问题:如何打开它们

我有一个主窗口(主视图)。如果用户点击显示按钮,然后选择演示窗口(模态对话框)应显示。什么是使用MVVM模式的preferable方法来创建和打开的窗口?我看到两种常用的方法:

第一个1(可能是最简单)。事件处理程序ShowButton_Click应在code来实现主窗口的背后方式是这样的:

 私人无效ModifyButton_Click(对象发件人,RoutedEventArgs E)
        {
            的ShowWindow WND =新的ShowWindow(anyKindOfData);
            布尔? RES = wnd.ShowDialog();
            如果(RES = NULL和放大器;!&安培; res.Value)
            {
                // ...店的变化,如果neecssary
            }
        }
 

如果我们显示按钮的状态应该改变(开启/关闭),我们将需要添加的逻辑,将管理按钮状态; 在源$ C ​​$ c是非常相似的旧式的WinForms和MFC源 - 我不知道这是好还是坏,请指教 在别的东西,我已经错过了?

另一种方法:

在Eclipse中创建新的重构功能 组图

在MainWindowViewModel我们将执行ShowCommand属性,将返回命令的ICommand接口。 Comman依次为:

将提高ShowDialogEvent; 将管理按钮状态。

此方法会更适合MVVM,但需要额外的编码:ViewModel类不能显示对话框,所以MainWindowViewModel只会提高ShowDialogEvent,我们将需要添加的事件处理程序在其MainWindow_Loaded方法MainWindowView,是这样的:

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

(ShowDialog的 - 类似ModifyButton_Click'方法)

所以,我的问题是:  1.你看到任何其他的方法呢?  2.你认为上市的一个是好还是坏? (为什么?)

任何其他的想法是值得欢迎的。

感谢。

解决方案

我在想这个问题最近也。这里有一个想法,我有,如果你使用统一在您的项目作为一个容器或其他依赖注入。我想通常你会覆盖 App.OnStartup()和创建模型,视图模型,并查看有,并给每一个适当的引用。使用Unity,你给容器中的参考模型,然后用集装箱来解析的观点。在统一容器注入您的视图模型,所以你不能直接实例化。一旦你的观点得到解决,你叫上显示()

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

我想你可以以某种方式嘲笑你的测试,调用统一容器的结果。或者,也许你可以写这样在App类,它基本上只是做我上面描述什么ShowMyChildView()的方法。这可能是很容易嘲笑调用 App.ShowMyChildView(),因为它只会返回一个布尔?,EH ?

嗯,这可能算不上是任何不只是使用更好的新MyChildView(),但它是一个有点想法我了。我想我会分享它。 =)

In the neighbour post: http://stackoverflow.com/questions/501886/wpf-mvvm-newbie-how-should-the-viewmodel-close-the-form/2100824#2100824 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.

解决方案

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

 
精彩推荐