UWP通过Webservice填写列表

时间:2016-01-14 14:05:13

标签: c# web-services uwp

我目前正在尝试使用来自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;
    }

当我单击按钮接收数据并显示它时,我的Textblock仅显示: 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),我不能再使用这种方法了。 有人能说出一个替代品吗?

1 个答案:

答案 0 :(得分:0)

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

您可能需要遵循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();
}