ViewModel 和 Model 之间使用 MVVM-Light 消息传递进行异步通信?
我开始为 WP7 编写一个应用程序,其中涉及通过 HTTPWebRequest 检索数据,我试图找到在异步请求返回数据后将数据返回到 ViewModel 的最佳方法。
Model 和 ViewModel 被分成各自命名空间中的单独项目,因此我不确定 MVVM-Light 是否可以以这种方式工作。或者我应该使用轻量级 IoC 实现和委托/事件来在模型中的调用返回后回调 ViewModel?
I am starting off writing an app for WP7 that involves retrieving data via an HTTPWebRequest, I am trying to find the best way to get the data back to the ViewModel once the async request has returned with the data.
The Model and ViewModel are split into seperate projects in their own namespaces, so am not sure if the MVVM-Light can work in this way. Or should I just use a lightweight IoC implementation and delegates/events to call back to the ViewModel once the call in the Model has returned?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下大小: http://amazedsaint.blogspot .com/2010/10/asynchronous-delegate-command-for-your.html。也非常适合测试,因为您的方法都不需要异步,您只需执行异步命令即可。
我对其进行了稍微编辑以扩展 DelegateCommand,以便我可以调用 RaiseCanExecuteChanged() 方法,如下所示:
希望有帮助。
Try this out for size: http://amazedsaint.blogspot.com/2010/10/asynchronous-delegate-command-for-your.html. Great for testing as well, as none of your methods need to be Asynchronous, you just execute your Async Command.
I edited it slightly to extend the DelegateCommand so I could call RaiseCanExecuteChanged() method, as below:
Hope that's of help.
您确定您在正确的位置提出请求吗?通常,模型仅包含您想要存储/传输的数据,视图模型将处理该模型属性的转换,以便视图可以显示(或编辑)该模型。我想模型对象是您将从 HttpWebRequest 中返回的对象,因此您可能需要考虑使用 Service 类来处理数据检索,尽管这对于您的应用程序来说可能有点过分了。
不管怎样,我强烈建议您在同一个地方处理请求和解析响应;否则,您会将逻辑分散到多个地方,这只会变得混乱且难以维护。
如果您需要一些了解 MVVM 的帮助,这里有一些资源可能会对
Are you sure you're making the request in the right place? Typically the Model contains simply the data that you want to store/transfer and the View Model would handle transforming that Model's properties so that a View can display (or edit) that Model. I would imagine that Model objects are what you're going to get back from the HttpWebRequest, and so you may want to consider using a Service class to handle retrieving the data, though that may be overkill for your application.
Either way, I'd highly reccomend that you handle the request and the parsing of the response in the same place; otherwise you are spreading the logic across multiple places, which is just going to get confusing and hard to maintain.
If you need some help understanding MVVM, here are a number of resources that you may find helpf