我尝试在异步操作之前更新我的UI组件,但是在执行异步任务后UI更新。按下按钮时执行以下代码:
Activity.RunOnUiThread(() => { changeButtonToUnFollow(); });
var friends = dbr.GetFollowers(user);
var friendship = friends.Where(f => f.UserName == loggedUser && f.Friend_UserName == user).FirstOrDefault();
dbr.DeleteFriend(friendship);
followerscount.Text = (Convert.ToInt16(followerscount.Text) - 1).ToString();
Task.Factory.StartNew(async () =>
{
await client.Follow(loggedUser, user, "true");
});
方法changeButtonToUnFollow()
是更新UI组件的方法。类"客户端"中的异步方法定义如下:
public async Task<string> Follow(string username, string friend_username, string unfriend)
{
var client = new HttpClient();
var response = await client.GetAsync(GlobalVariables.host + "api/member/AddFriend/" + username + "/" + friend_username + "/" + unfriend).Result;
return response.ToString();
}
如何在进行异步调用之前更新UI?