【发布时间】:2018-04-18 23:00:18
【问题描述】:
我正在使用 Xamarin Forms 和 MVC API .net 实现 Web API 服务。我已经创建了身份验证服务,并且可以使用 Postman 应用程序获取访问令牌。如果我在 UWP 平台上运行我的项目,该应用程序运行良好,我可以让用户访问 Web API 的令牌。但是,每当我在 Android 平台上运行该应用程序时,我都会收到一个未处理的异常,指出“跳过了 36 帧。应用程序可能在其主线程上做了太多工作”。我正在使用 Async ... Await 方法,但仍然出现此错误。你能告诉我如何避免这个错误吗?谢谢!
这是我的代码:
ApiServices:
public class ApiServices {
public async Task RegisterUserAsync(string email, string password, string confirmPassword) {
var client = new HttpClient();
var success = false;
var model = new RegisterBindingModel {
Email = email, Password = password, ConfirmPassword = confirmPassword
};
try {
var json = JsonConvert.SerializeObject(model);
HttpContent content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
if (email != null || password != null) {
success = true;
Acr.UserDialogs.UserDialogs.Instance.ShowSuccess(string.Format("You are now signed-in as {0}.", email));
var response = await client.PostAsync(Constants.BaseApiAddress + "api/Account/Register", content);
Debug.WriteLine(response);
Debug.WriteLine(await response.Content.ReadAsStringAsync());
Debug.WriteLine(response.StatusCode);
}
} catch (Exception ex) {
//Acr.UserDialogs.UserDialogs.Instance.ShowError(string.Format("Authentication Failed: {0}", ex.Message));
}
}
}
LoginViewModel:
public class LoginViewModel {
public string Email {
get;
set;
}
public string Password {
get;
set;
}
public ICommand LoginCommand {
get {
return new Command(async() => {
ApiServices apiServices = new ApiServices();
await apiServices.LoginUserAsync(Email, Password);
});
}
}
}
【问题讨论】:
-
通过
await Task.Run(....);将所有代码推送到线程中
标签: c# asp.net-web-api xamarin xamarin.forms cross-platform