SilverLight 中调用wcf service 的问题

jxzhang615 2010-08-02 10:27:13
在silverlight程序中。我有一个class:rsmservice。该class有一个方法 GetAppointments

我在xaml的cs文件中调用 rsmservie.GetAppointments方法得到一个string。


现在的问题是:GetAppointments不是直接返回的一个string,而是调用的wcf service 得到一个值

wcf service都是异步调用。我在view中怎么得到这个返回值呢?

请高人赐教啊。谢谢先。。。。
...全文
156 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jv9 2010-08-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sunpire 的回复:]
假设LZ已经熟悉了异步调用WCF。

方法有多种,举两种常用的:一种就是如果 GetAppointments() 所返回的结果是不变的,
那么可以在使用这个 rsmservice 之前,先通过调用WCF获得这个string,并将结果赋值给 rsmservice 中的一个 static 变量,GetAppointments() 则变成是从这个 static 变量中取值。----这种方式 rsm……
[/Quote]


正解。。。。
Alfredknox 2010-08-03
  • 打赏
  • 举报
回复
采用异步模式,由于在WCF服务的配置中我们采取了BasicHttpBinding,客户端也要采用BasicHttpBinding。我们需要注册GetPostsCompleted事件处理方法,以便完成后回调,同时调用GetPostsAsync()方法获取数据。如:
public partial class Page : UserControl { public Page() { InitializeComponent(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { Binding binding = new BasicHttpBinding(); EndpointAddress endPoint = new EndpointAddress( "http://localhost:52424/Blog.svc");
BlogClient client = new BlogClient(binding, endPoint); client.GetPostsCompleted += new EventHandler<GetPostsCompletedEventArgs>(client_GetPostsCompleted); client.GetPostsAsync(); } void client_GetPostsCompleted(object sender, GetPostsCompletedEventArgs e) { if (e.Error == null) { Posts.ItemsSource = e.Result; } } }
badi888 2010-08-02
  • 打赏
  • 举报
回复
在回调函数里获得返回值
Sunpire 2010-08-02
  • 打赏
  • 举报
回复
假设LZ已经熟悉了异步调用WCF。

方法有多种,举两种常用的:一种就是如果 GetAppointments() 所返回的结果是不变的,
那么可以在使用这个 rsmservice 之前,先通过调用WCF获得这个string,并将结果赋值给 rsmservice 中的一个 static 变量,GetAppointments() 则变成是从这个 static 变量中取值。----这种方式 rsmservice 中不封装 WCF 的代理类,一般针对于基础数据先用这种方式,即把基础数据获取到Silverlight端后再使用它们。

另一种则是让 rsmservice 实现System.ComponentModel.INotifyPropertyChanged指口,
同时新增一个一属性,如 public string Appointments{get;set;},
在WCF异步调用完成时,赋值给 Appointments 属性,并触发 PropertyChanged 事件。
前台View则绑定这个Appointments 属性,使用TwoWay绑定。----这种方式 rsmservice 中封装 WCF 的代理类,由GetAppointments() 调用WCF的方法,注意在异步调用完成之前Appointments 属性为null。
暖枫无敌 2010-08-02
  • 打赏
  • 举报
回复
建议将该GetAppointments方法,写在WCF里或Web Service里面,生成客户端代理后,调用获取数据。
ztmdsbt 2010-08-02
  • 打赏
  • 举报
回复
一般是在方法的Complated事件中,e.Result的值就是返回值了.

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧