Xamarin异步方法的问题

时间:2015-12-09 15:29:48

标签: c# wcf asynchronous xamarin xamarin-studio

如果我的APP上有一个按钮,我在Android模拟器上运行了以下代码,但是当我将应用程序传递到手机时会生成一条消息,应用程序停止运行。

    Button buttonPessoasNecessita = FindViewById<Button> (Resource.Id.mybuttonPesNec);
    buttonPessoasNecessita.Click += delegate {
    Service1Client client;
    var binding = new BasicHttpBinding () { Name= "basicHttpBinding", MaxReceivedMessageSize = 67108864,};

    binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() 
    { MaxArrayLength = 2147483646, MaxStringContentLength = 5242880, }; 

    var timeout = new TimeSpan(0,60,60); 
    binding.SendTimeout= timeout; 
    binding.OpenTimeout = timeout; 
    binding.ReceiveTimeout = timeout; 


    client = new Service1Client(binding, new EndpointAddress ("http://engb.uni5.net/Service1.svc")); 

    buttonPessoasNecessita.text = client.envioPessoa();

    };

试图解决被认为异步使用的情况,我尝试了各种各样的例子,但没有一个对我变得清楚,我看到我的对象'client'是有一个方法有beginenvioPessoa()方法和其他endenvioPessoa();基于此链接使用async / await的简单方法使用WCF服务尝试设置一种消耗Web服务异步方式的方法

调用方法:

 var t = await executeAsync(binding);

方法:

public async Task<string> executeAsync(BasicHttpBinding binding){

    Service1Client client = new Service1Client(binding, new EndpointAddress ("http://engb.uni5.net/Service1.svc")); 

    var t = Task<string>.Factory.FromAsync (
        ((IService1)client.InnerChannel).BeginenvioPessoa,
        ((IService1)client.InnerChannel).EndenvioPessoa);
    return await t;
}
  

错误CS1502:最佳重载方法匹配   “System.Threading.Tasks.TaskFactory.FromAsync(System.IAsyncResult,   System.Func)'有一些无效的参数   (CS1502)

     

错误CS1503:参数1:无法从'方法组'转换为   'System.IAsyncResult'(CS1503)

1 个答案:

答案 0 :(得分:0)

签名是:

Task<string>.Factory.FromAsync(IAsyncResult asyncResult, Func<IASyncResult, string> endMethod)

所以试试这个

var t = Task<string>.Factory.FromAsync( 
  ((IService1)client.InnerChannel).BeginenvioPessoa, result =>
    ((IService1)client.InnerChannel).EndenvioPessoa);