在 MVVM (WPF) 中命令——如何返回一个值? [英] Commanding in MVVM (WPF)--how to return a value?

查看:51
本文介绍了在 MVVM (WPF) 中命令——如何返回一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 MVVM 模式有一段时间了,但在现实生活中我仍然遇到问题.这是另一个:我使用命令并冒泡在 ViewModel 中处理要处理的事件.到现在为止还挺好.但是我使用 MVVM 的项目实际上是一个类库.运行命令代码后,我需要能够将对象发送回调用应用程序.有什么建议的方法可以这样做?

I've been using MVVM pattern for a while now, but I still run into problems in real-life situations. Here's another one: I use commanding and bubble up the event to be handled in the ViewModel. So far, so good. But the project for which I'm using MVVM is actually a class library. Once I run the command code, I need to be able to send an object back to the calling application. What are suggested ways to do so?

具体来说:在我的调用应用程序中,我有一个 XAML 页面直接绑定到库的 ViewModel,其中包含一个对象Thing1".单击按钮时,将调用 ViewModel 中的一个方法(称为CopyThing1()").它复制Thing1"以创建Thing2".然后我需要将Thing2"发送回调用应用程序.

Specifically: In my calling app I have a XAML page bound directly to the library's ViewModel, which contains an object "Thing1". When a button is clicked, a method in the ViewModel is called (call it "CopyThing1()"). It copies "Thing1" to create "Thing2". Then I need to send "Thing2" back to the calling app.

谢谢!!!

推荐答案

尽管 Commanding 上的信息清晰且正确,但它无法应用于我的情况,因为需要发生的响应是在不使用 MVVM 的调用应用程序中它不仅仅是 UI 响应.我确实研究了 Prism,但发现它对于我目前需要的东西来说太复杂了.我最终引发和处理事件,如here所述-->WPF MVVM 在视图上触发事件的正确方法视图模型

Although the info on Commanding was clear and correct, it couldn't be applied in my case because the reponse required to happen was in a calling application NOT using MVVM and it was not to be UI response only. I did investigate Prism, but found it too complex for what I need at the moment. I ended up raising and handling events, as described here--> WPF MVVM Correct Way to Fire Event on View From ViewModel

这篇关于在 MVVM (WPF) 中命令——如何返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆