如何从其他 Viewmodel 获取答案
SL4 VS2010 MVVM-light c#,信使。
我有一个父 userControl 调用 子用户控件。 (两者都有 ViewModels)
我正在使用 MVVM-light 中的信使。(子级是父级选项卡中的用户控件)
当子级需要关闭时,我们必须检查父级 ViewModel 上的某些规则 (父结果是您可以关闭)。
您将如何与子 ViewModel 通信父 ViewModel?
给孩子发消息给家长询问我可以关闭吗? 然后,向父母发送消息以返回“是”或“否”,您可以
(我?不喜欢来回发送两次消息)
我真正想知道的是是否有其他方法可以在 ViewModel 之间进行类似的通信?
或者如果父级规则不允许子级关闭,如何将子级上的关闭按钮变暗。
感谢您抽出时间。
SL4 VS2010 MVVM-light c# ,messenger.
I have a parent userControl calling
a child userControl. (both have
ViewModels)I am using messenger from MVVM-light.(the child is a usercontrol in a Tab of the parent)
When the child needs to close we must check certain rules on the parent-ViewModel
(the parent result is you can close on not).
How would you communicate the parent-ViewModel from the child-ViewModel?
message child to parent asking can I close?
then, message the parent to child to return yes or no you may
(me? don't like to message twice back and forth)
what I really want to know is if there is any other way to preform communication like these among ViewModels?
or how about dimmed the close button on child if the parent rules don't allow the child to close.
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够使用 MVVM-Light 中的 Messenger 来完成此任务。
子窗口发布消息通知,如下所示
您可以定义操作回调,如下所示
您可以在父窗口上订阅此通知,如下所示
如果这有帮助,请告诉我。
You should be able to use Messenger in MVVM-Light to achieve this task.
The child window publishes the messenger notification as shown below
You can define the Action callback as shown below
You can subscribe for this notification on parent as shown below
Please let me know if this helps.
很难说,但从您所描述的情况来看,听起来您在父视图模型中有一些属于模型的规则。如果您将这些规则推入模型中,则父虚拟机和子虚拟机都可以使用这些规则。
但是,如果情况并非如此,您始终可以拥有某种发布/订阅关系,父级和子级都可以注册并使用这些事件进行通信。
我倾向于模型方法。
It's hard to say, but from what you've described, it sounds like you have some rules in the parent view model that belong in the model. If you pushed those rules into the model, both the parent and the child VMs could use those.
However, if that's not the case, you could always have some sort of publish/subscribe relationship that both the parent and the child register with and use those events to communicate.
I'd lean towards the model approach.
您有多种选择,但最好的答案可能是最适合应用程序整体设计的其余部分的选择。尚未提出的一个选项是您的子 ViewModel 可以引用父 ViewModel(这可以在打开子视图的原始消息中提供),并且子 ViewModel 可以询问父 ViewModel通过方法或属性需要的任何问题。
来自 child-ViewModel 的示例查询:
如果您想使用来自多种类型的父级的子级,那么您可以创建一个接口,以确保实现者拥有始终可以从子级调用的 CanSave 方法。
这只是一种选择,无论您是使用消息传递还是直接 ViewModel 到 ViewModel 创建和通信,它都可以工作。主要思想是,如果您知道父级应该始终实现某些功能,则不要创建回调,而是在消息中提供该事实,将其强制作为消息契约的一部分。那些使用消息创建子级的人必须提供实现接口的东西,并且保证子级有可用的东西以强类型和编译时检查的方式调用。
You have several options but the best answer is probably the one that fits the best in the rest of the overall design of your application. One option that hasn't been brought up is that your child-ViewModel could have a reference to the parent-ViewModel (this could have been provided within the original message that opened the child) and the child-ViewModel can ask the parent-ViewModel any question it needs via a method or property.
Example query from child-ViewModel:
If you would like to use this child from several types of parents then you could create an interface which ensures implementers have a CanSave method that can always be called from the child.
This is just one option and it would work whether you are using messaging or direct ViewModel to ViewModel creation and communication. The main idea is that instead of creating callbacks that if you know the parent should always implement some functionality then providing that fact in the message enforces this as part of the message contract. Those who create the child using the message would have to provide something that implements the interface and the child would be guaranteed to have something available to call into in a strongly typed and compile time checked way.