我正在使用xamarin处理有关iphone应用程序开发的一些视频教程,我似乎遇到了异步问题。
我有一个按钮,当点击时会运行一个生成http请求的异步方法。但是,当单击时,异步方法永远不会运行,当我检查状态时它会显示“waitingforactivation”
public class RootViewModel : UIViewController
{
private UIButton _button;
private string response;
public override void ViewDidLoad()
{
base.ViewDidLoad();
Title = "Load Async";
View.BackgroundColor = UIColor.DarkGray;
_button = new UIButton(UIButtonType.RoundedRect);
_button.Frame = new RectangleF(0, View.Bounds.Height - 60, View.Bounds.Width, 60);
_button.BackgroundColor = UIColor.FromRGB(0, 0, 0);
_button.SetTitle("Get Async", UIControlState.Normal);
_button.SetTitleColor(UIColor.FromRGB(255, 255, 255), UIControlState.Normal);
response = string.Empty;
_button.TouchUpInside += async (sender, args) =>
{
response = await DoStuffAsync();
Title = response;
};
View.AddSubview(_button);
}
private async Task<string> DoStuffAsync()
{
var url = @"http://www.google.co.uk";
using (var client = new HttpClient())
{
var response = await client.GetStringAsync(url);
return response.Substring(0, 10);
}
}
}
非常感谢任何有关失败原因的帮助。