Xamarin.android 带monodroid的异步WCF调用

Xamarin.android 带monodroid的异步WCF调用,xamarin.android,Xamarin.android,我当前调用WCF的方法是bog标准事件,异步样式(例如) 这很好,但有时速度非常慢,而且很麻烦,因为您需要另一种方法来处理结果 有没有一种方法可以更接近Win8上的编码方式 private async foo<bool>() { try { await foo.EventArgsAsync(params...) } catch { // catch here } // deal with the code back retur

我当前调用WCF的方法是bog标准事件,异步样式(例如)

这很好,但有时速度非常慢,而且很麻烦,因为您需要另一种方法来处理结果

有没有一种方法可以更接近Win8上的编码方式

private async foo<bool>()
{
  try 
  {
     await foo.EventArgsAsync(params...)
  } 
  catch
  {
     // catch here
  }

  // deal with the code back
  return true;
}
private async foo()
{
尝试
{
等待foo.EventArgsAsync(参数…)
} 
抓住
{
//抓住这里
}
//处理回代码
返回true;
}
谢谢

保罗,你可以

如果代理具有
*开始
/
*结束
方法,则可以使用
任务工厂。fromsync

public static Task<int> FooTaskAsync(this FooClient client)
{
  return Task<int>.Factory.FromAsync(client.BeginFoo, client.EndFoo, null);
}

谢谢。与我最初使用的方式相比,这会有很大的速度差异吗?不会。您正在用一个异步API包装另一个异步API;如果有的话,它会稍微慢一点。这样做的好处是基于
任务
的API可以无缝地与
异步
配合使用,因此您的代码更加干净。
public static Task<int> FooTaskAsync(this FooClient client)
{
  return Task<int>.Factory.FromAsync(client.BeginFoo, client.EndFoo, null);
}
public Task<int> FooTaskAsync(this FooClient client)
{
  var tcs = new TaskCompletionSource<int>();
  client.FooCompleted += (s, e) =>
  {
    if (e.Error != null) tcs.SetException(e.Error);
    else if (e.Cancelled) tcs.SetCanceled();
    else tcs.SetResult(e.Result);
  };
  client.FooAsync();
  return tcs.Task;
}