如何使用 MVVM 打开对话框窗口
有人可以帮我解决如何打开对话框窗口,
我能想到的最简单的场景是: 我们有一个带有按钮和标签的主窗口,
当用户按下该按钮时,
会出现一个带有文本框和2个按钮的对话框窗口,其中
一个按钮表示提交,
当用户按下提交时,它会关闭窗口,
并更改颜色将主窗口背景设置为红色,
并将输入放在文本框中,并将主窗口上的标签更改为该内容(我非常担心这部分,我知道如何执行此部分),
而另一个按钮只是取消操作,
假设MainWindow和DialogWindow的Datacontext分别是MainWindowViewModel和UserInputViewModel。
现在,在此链接上,Cameron 谈论使用服务,即 IDialogService 和 DialogService 现在有人可以向我解释如何在上面的场景中实现这些方法吗?或者如果有其他方法可以做到这一点,请告诉我? 请不要将我链接到任何页面,因为我可能已经阅读了所有这些页面,但我似乎无法清楚地了解要发生的事情? ~慢慢失去理智,因为 MVVM 让事情变得更加困难:(
Can someone please help me work out how to open a Dialog Window,
the simplest scenario I can think of is:
We have a main window with a button and a Label,
when the user presses that button,
a dialog window with a text box and 2 buttons appear,
one button says submit,
when the user presses submit it closes the window,
it changes the color of the mainwindows background to red,
and takes the input placed in the textbox and changes a label on the main window to that content(I am bot so much worried about this part I get how to do this part),
while the other button just cancels the operation,
Assume that the Datacontext of the MainWindow and DialogWindow is MainWindowViewModel and UserInputViewModel respectivily.
Now on this link Cameron talks about using a service, ie IDialogService and DialogService
now could someone please explain to me how to implement those methods in the scenario above? Or if there is another way to do this then please let me know?
Please don't link me to any to any pages because I've probably read them all and I can't seem to get a clear understanding of what is meant to be happening?
~Slowly loosing his sanity because MVVM makes things so much harder :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不是真正的答案,但我想我还是会添加我的 POV。如何以 MVVM 方式使用对话框,目前还没有人真正能够以一种优雅的方式做到这一点。基本上有3个营地:
(1) 使用您所描述的对话服务的人,
(2) 那些认为 MVVM 很好,但不值得花费无数个小时尝试正确的东西的人,因此他们使用代码隐藏,并且
(3) 像我这样的人,他们认为对话框和父视图通常紧密地联系在一起,他们应该共享视图模型(例如,对话框只是显示对话框的一种方式)来自您的视图模型的数据)。
Not really an answer, but I think I'll add my POV anyway. How to use dialogs in an MVVM way, is something no one has really managed to do in an elegant fashion yet. There's basically 3 camps:
(1) The people who use an dialogservice like you described,
(2) The people who thinks that MVVM is good, but not something you should spent countless of hours trying to get right, so they use the codebehind, and
(3) people like me, who thinks that more often than not, the dialog and the parentview are so tied together that they should share viewmodels (as-in, the dialog is just one way of showing the data from your viewmodel).
当尝试做一些比简单数据绑定更高级的事情时,MVVM 的学习曲线可能会稍微陡峭一些。您检查过 MVVM Light 工具包吗?它包括一个 Messenger 类,可以方便地在各处发送消息。监听者注册他们想要的消息,发送者只需发布它们。通过这种方式,听者和发送者都不知道对方,但可以进行通信。这意味着 View 可以注册消息,而 ViewModel 可以发送消息。
这个问题谈论的是做一些与你想做的事情非常相似的事情。顺便推荐一下 MVVM Light 工具包!
我不确定如何使用对话框的结果将它们发送到 ViewModel。我假设对您来说更困难的部分是从虚拟机到视图的通信。
The learning curve for MVVM can be slightly steeper when attempting to do something a little more advanced than simple data binding. Have you checked out the MVVM Light toolkit? It includes a Messenger class that facilitates sending messages around the place. A listener registers for messages they want, and a sender just publishes them. In this manner, neither the listener or sender know about each other, but can communicate. Meaning the View can register for a message, and the ViewModel can send one.
This question talks about doing something very similar to what you want to do. I recommend the MVVM Light toolkit by the way!
I'm not sure about how you use the results of a dialog to send them through to the ViewModel. I'm assuming the harder part for you is communicating from the VM to the View.