良好的体系结构在为Windows Phone 7的应用程序MVVM消耗REST Web服务? [英] Good architecture for consuming a REST web service in an MVVM app for Windows Phone 7?

查看:160
本文介绍了良好的体系结构在为Windows Phone 7的应用程序MVVM消耗REST Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有点麻烦决定的最佳方法从Web服务到UI获取数据。

由于Web客户端的异步特性你将如何构建这个


  • 示范使用Web客户端谈谈web服务

  • 视图模型要求数据模型

  • 查看被数据绑定到视图模型

在我需要得到这些数据背出模型视图模型的异步complete事件,这些是我想过的事情。


  1. 我可以发射,该视图模型订阅模型的事件。

  2. 我也许可以做一些与身边掠过回调?

  3. 或者我应该做的视图模型和模型?之间INotifyPropertyChanged的事件的第二级

  4. 还是我很迷茫,完全误解MVVM?


解决方案

这取决于纯粹的你想怎么约为MVVM。

您可以把API本身作为你的模型,在这种情况下,视图模型具有Web客户端和异步完成你设置你的属性(和它们将依次从他们的setter内火灾的PropertyChanged)。

或者你可以有一个在它的WebClient code(因为它听起来像你有)的本地模型。在这种情况下,我个人的做法将有一个ModelUpdated事件,从异步完整的事件触发。 (您的选项1)。

您的ViewModel可以侦听此事件,并且会触发一个的PropertyChanged(空)有观要求的所有属性,或火灾多的PropertyChanged事件。记住,你不是仅限于从setter方法​​中射击的PropertyChanged。没有什么东西像有一个方法阻止你。

 私人无效FireMultipleProperties(){
NotifyPropertyChanged(Property1);
NotifyPropertyChanged(Property2);
NotifyPropertyChanged(Property3);
}

所以,你可以调用这个方法时,模型填充完毕,你的UI将调用,当他们被解雇更新各个特性。你只需要做到这一点,如果你有一吨的属性,不希望与一次解雇他们所有的的PropertyChanged(空)

I'm having a bit of trouble deciding on the best way to get the data from the web service to the UI.

Given the asynchronous nature of WebClient how would you build this?

  • Model uses WebClient to talk to webservice
  • ViewModel asks model for data
  • View is databound to ViewModel

On the Async complete event I need to get that data back out of the model to the ViewModel, these are the things I've thought about.

  1. I could fire an event in the Model that the ViewModel subscribes to.
  2. I could perhaps do something with passing around callbacks?
  3. Or should I be doing a second level of INotifyPropertyChanged events between the ViewModel and the Model?
  4. Or am I very confused and completely misunderstanding MVVM?

解决方案

It depends on how purist you want to be about MVVM.

You could treat the API itself as your Model, in which case the ViewModel has the WebClient and on Async completed you'd set your properties (and they would in turn fire PropertyChanged from within their setters).

Or you can have a local Model that has the WebClient code in it (as it sounds like you have). In this case, my personal approach would to have a "ModelUpdated" event that fires from the Async complete event. (Your option 1).

Your ViewModel can listen for this event, and either fire a PropertyChanged(null) to have the View ask for ALL properties, or fire multiple PropertyChanged events. Remember you're not restricted to firing PropertyChanged from within your setters. There's nothing stopping you from having a method like

private void FireMultipleProperties(){
NotifyPropertyChanged("Property1");
NotifyPropertyChanged("Property2");
NotifyPropertyChanged("Property3");
}

So you could call that method when the Model finishes populating, and your UI will call update each property when they are fired. You'd only need to do this if you have a ton of properties and don't want to fire them all at once with PropertyChanged(null).

这篇关于良好的体系结构在为Windows Phone 7的应用程序MVVM消耗REST Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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