UWP 通过网络服务填充列表

本文关键字:填充 列表 网络服务 UWP | 更新日期: 2023-09-27 18:30:34

我当前正尝试使用来自 Web 服务的数据填充我的 UWP 项目中的列表。我测试了这段代码:

public void test()
{
       BasicHttpBinding basicAuthBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
        basicAuthBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        EndpointAddress basicAuthEndpoint = new EndpointAddress("myURI");
        LiveOdi.getODI_v1_PortTypeClient ptc = new LiveOdi.getODI_v1_PortTypeClient(basicAuthBinding, basicAuthEndpoint);
        ptc.ClientCredentials.UserName.UserName = "myUsername";
        ptc.ClientCredentials.UserName.Password = "myPasswort";
        ptc.InnerChannel.OperationTimeout = new TimeSpan(0, 0, 1);
        string = ptc.getODIAsync("1").ToString();
}
    private void button_Click(object sender, RoutedEventArgs e)
    {
        test();
        textBlock.Text = string;
    }

当我点击按钮接收数据并显示它时,我的文本块只显示:System.Threading.Tasks.Task'1[Test.LiveOdi.getODIResponse]

相同的代码在 datagrid.datasource 设置为 ptc 的表单中工作正常。GetODI("1").

编辑:由于错误(https://social.msdn.microsoft.com/Forums/sqlserver/en-US/84024ccf-7ef2-493e-a7bb-c354f42a354d/does-uwp-10-support-soap-services?forum=wpdevelop),我不能再使用这种方法了。有人可以说出一个替补吗?

UWP 通过网络服务填充列表

ptc.getODIAsync("1") 是异步的,并且返回一个Task。这就是为什么它显示在TextBox中(因为您的代码在Task上调用ToString)。

您可能需要遵循async/await模式,以便获得响应。

public async Task<string> test()
{
     // put all of the web service setup code here, then:
     string result = await ptc.getODIAsync("1");
     return result; 
}
// add async here so that the Click event can use Tasks with await/async
private async void button_Click(object sender, RoutedEventArgs e)
{
    // stuff the value when the response returns from test
    textBlock.Text = await test();
}