【发布时间】:2014-03-06 12:20:01
【问题描述】:
重构此代码以使用 async/await 的最佳方法是什么?这段代码sn-p来自Xamarin Field Services Sample App
ViewModel 的数据提供者接口
public interface ILoginService {
Task<bool> LoginAsync (string username, string password, CancellationToken cancellationToken = default(CancellationToken));
}
登录接口实现。这里只是为了假网络电话睡觉......
public class LoginService : ILoginService {
public Task<bool> LoginAsync (string username, string password, CancellationToken cancellationToken = default(CancellationToken)) {
return Task.Factory.StartNew (() => {
Thread.Sleep (1000);
return true;
}, cancellationToken);
}
}
按钮点击处理程序
partial void Login () {
//some ui related code
loginViewModel
.LoginAsync ()
.ContinueWith (_ =>
BeginInvokeOnMainThread (() => {
//go to different view
}));
}
重构此代码以使用 async/await 的最佳方法是什么?
【问题讨论】:
标签: c# ios asynchronous xamarin.ios xamarin