开发者

MVVM Light requires a reference to System.Windows in my ViewModel?

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.Window开发者_高级运维s, 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


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


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜