0
我正在開發Windows 8的應用程序。我有一個名爲「User」的C#類。用戶有一個稱爲認證的方法。我的方法看起來像這樣:在WinRT中檢測方法的完成時間(C#)
public class User
{
public bool IsAuthenticated { get; set; }
public async void Authenticate(string username, string password)
{
// Code to build parameters and url is here
var response = await httpClient.PostAsync(myServiceUrl, new StringContent(json, String.Text.Encoding.UTF8, "application/json"));
JsonObject result = JsonObject.Parse(await response.Content.ReadAsStringAsync());
}
}
Authenticate方法的工作原理。它成功擊中我的服務並返回適當的細節。我的問題是,如何檢測此方法何時完成?我在調用這個方法來響應用戶點擊我的應用中的「登錄」按鈕。例如,像這樣的:
private void loginButton_Click(object sender, RoutedEventArgs e)
{
User user = new User();
user.IsAuthenticated = false;
user.Authenticate(usernameTextBox.Text.Trim(), passwordBox.Password.Trim());
if (user.IsAuthenticated)
{
// Allow the user to enter
}
else
{
// Handle the fact that authentication failed
}
}
本質上,我需要等待authenticate方法來完成其執行。但是,我不知道該怎麼做。我究竟做錯了什麼?
謝謝