使用await client.GetAsync(uri)
阻止我的UI线程。
这是我的按钮点击事件处理程序中的代码:
private async void Button_Clicked(object sender, EventArgs e)
{
var vm = new MainPageViewModel();
HttpClient client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
var uri = new Uri(string.Format("http://localhost:50715/api/values"));
var response = await client.GetAsync(uri); // This is where UI gets blocked
SongView.ItemsSource = new List<Song>();
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
vm.Songs = await Task.Factory.StartNew(() =>
{
return JsonConvert.DeserializeObject<List<Song>>(content);
});
}
SongView.ItemsSource = vm.Songs;
}
即使只使用此代码,它仍然会阻止同一行的UI。
private async void Button_Clicked(object sender, EventArgs e)
{
HttpClient client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
var uri = new Uri(string.Format("http://localhost:50715/api/values"));
var response = await client.GetAsync(uri); // This is where UI gets blocked
}
你能指出我做错了吗?