MVVM Light 需要在我的 ViewModel 中引用 System.Windows?

发布于 2024-09-07 07:58:51 字数 463 浏览 1 评论 0原文

我的目标是让 ViewModel 告诉 View 显示 MessageBox 并向用户询问问题。

我不希望 ViewModel 有任何对 Windows 特定代码的引用。

我花时间下载并安装 MVVM Light Toolkit。现在我发现即使使用该工具包,我的 ViewModel 也必须引用 System.Windows,才能解决 MessageBoxButton 和 MessageBoxResult 之类的问题。

当您最终得到一个引用 System.Windows 的 ViewModel 时,为什么还要进行所有这些额外的工作呢?我还可以直接从 ViewModel 调用 MessageBox.Show() 。本质上,我使用 MVVM 工具包没有保存任何内容,所以我只是不明白为什么要使用它。

有人可以向我解释一下,如果我仍然需要在 ViewModel 中引用 System.Windows,为什么要进行这项工作?

谢谢,休

My goal was to enable the ViewModel to tell the View to present a MessageBox and ask the user a question.

I didn't want the ViewModel to have any reference to Windows-specific code.

I take the time to download and install MVVM Light Toolkit. Now I find that even with the toolkit, it is necessary for my ViewModel to reference System.Windows, in order to resolve things like MessageBoxButton and MessageBoxResult.

Why go through all this extra work, when you just wind up with a ViewModel that has a reference to System.Windows? I might as well call MessageBox.Show() straight from the ViewModel. Essentially I have saved nothing by using the MVVM toolkit, so I just don't understand why I would use this.

Can someone please explain to me, why go through this effort, if I still have to reference System.Windows in my ViewModel?

Thanks, Hugh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风尘浪孓 2024-09-14 07:58:51

当您使用MessageBox类时,您需要引用System.Windows.Forms.dll。 MVVM-Light 不需要此参考。

When you use MessageBox class, you need a reference to System.Windows.Forms.dll. MVVM-Light does NOT require this reference.

掩耳倾听 2024-09-14 07:58:51

它并没有违背该工具包的目的。该工具包提供了一个轻量级框架,用于将视图与模型分开,这对于单元测试绝对有帮助。

他们重用了枚举,因此不必重写它们。重用特定于 UI 的枚举确实看起来有点奇怪,但至少它们不是事件处理程序和事件参数。

最后,MVVM 使用命令,这需要实现 ICommand< /a> 接口。该界面位于PresentationCore 中,并且更加针对UI :)

总体而言,目的不是删除引用,而是尽可能不使用这些引用,并且枚举绝对不会破坏该模式。

It doesn't defeat the purpose of the toolkit. The toolkit provides a light framework for separating your view from your model, which is definitely helpful with unit testing.

They reused the enums, so they didn't have to rewrite them. It definitely does seem a bit strange to reuse UI specific enums, but at least they aren't event handlers and event arguments.

Lastly, MVVM uses commands, which requires the implementation of ICommand interface. That interface is in the PresentationCore and is even more UI specific :)

Overall, the purpose isn't to remove references it is to not use those references as best you can and enums definitely don't break the pattern.

半透明的墙 2024-09-14 07:58:51

mvvm light 工具包可以帮助您实现 mvvm 模式。 mvvm light 工具包还具有可混合的真正优点(为您进行了一些调整,以便您可以使用 Expression Blend 非常轻松地进行设计)。 Mvvm 模式也确实有助于(正如其他人所说)应用程序的测试,因为测试视图很困难,但测试 viewModel 却容易得多。

如果您想知道为什么要进行这项工作并使用 mvvm light 我建议您检查 这个相关答案

希望它有所帮助,祝你好运!

The mvvm light toolkit is there to help you implement a the mvvm pattern. The mvvm light toolkit also has the really nice advantage of being blendable, (some tweaks are made for you so you can use Expression Blend to design very easily). The Mvvm pattern also really helps (as others said) the testing of your application as it is hard to test the view but it is a lot easier to test the viewModel.

If you want to know why you go through this effort and use mvvm light I suggest you check this related answer

Hope it helps and good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文