2012-04-20 108 views
0

我在后端web服务中编写了一个简单的WebMethod。我将它用作WPF应用程序和Silverlight应用程序中的服务引用。使用Web服务与Silverlight和WPF之间的区别

该方法返回一个List<string>,调用userList。这在WPF应用程序中运行良好,我在其中将Service1SoapClient称为“客户端”。有前调用该方法通过 -

client.userlist(); //this is the case in WPF app 

但是在Silverlight中唯一的选择是

client.userListAsync(); //Silverlight 

这在WPF工作正常,并带回所需的列表,但是Silverlight为后面的错误 -

Error 11 Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>' 

也与此有关,在WPF应用程序中,我使用userList附加了带有richTextBox的文本,该文件可以工作,但是在Silverlight中richTextBox1.AppendText不是val ID选项。

我在Silverlight应用程序中出错的地方?

回答

3

Silverlight中的所有Web服务调用都是异步的,这意味着您在等待结果返回时无法使应用程序块执行。相反,你告诉Silverlight在获得结果时应该怎么做,并让它继续自己的业务,直到那时。

您的Silverlight应用程序的Web服务客户端需要您传递一个事件处理函数,它将Web方法的返回值作为xxxCompletedEventArgs参数,其中“xxx”是Web方法的名称。

此页面:http://msdn.microsoft.com/en-us/library/cc197937(v=vs.95).aspx告诉您如何设置您的事件处理程序并使用它来处理Web服务调用的输出。

从页面:

proxy.GetUserCompleted += new EventHandler<GetUserCompletedEventArgs (proxy_GetUserCompleted); 
    proxy.GetUserAsync(1); 
    //... 
} 

//... 

void proxy_CountUsersCompleted(object sender, CountUsersCompletedEventArgs e) 
{ 
    if (e.Error != null) 
    { 
     userCountResult.Text = “Error getting the number of users.”; 
    } 
    else 
    { 
     userCountResult.Text = "Number of users: " + e.Result; 
    } 
} 
+0

请务必取消与相同的代理对象,您将在每次执行执行proxy_CountUsersCompleted再次下一次执行,否则注册事件处理程序。 – Stainedart 2012-04-20 17:39:54

+0

优秀的答案。链接正是我所需要的。作品一种享受。 – Ebikeneser 2012-04-21 14:01:20

相关问题