0

WebMethod在后端 Web 服务中编写了一个简单的。我在 WPF 应用程序和 Silverlight 应用程序中都使用它作为服务引用。

该方法返回一个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 的文本,这可以工作,但是在 SilverlightrichTextBox1.AppendText中不是一个有效的选项。

Silverlight 应用程序哪里出错了?

4

1 回答 1

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;
    }
}
于 2012-04-20T12:49:23.033 回答