当我调用此方法时,什么也没有发生,应用程序崩溃。我认为这是由于ExecuteAsync
方法。有人可以帮助我吗?这是我的代码。
代码1:
public Task<Connection> Connect(string userId, string password)
{
var client = new RestClient(_baseUrl)
{
Authenticator = new SimpleAuthenticator("user", userId,
"password", password)
};
var tcs = new TaskCompletionSource<Connection>();
var request = new RestRequest(AppResources.Authenticating);
client.ExecuteAsync<Connection>(request, response => tcs.SetResult(new
JsonDeserializer().Deserialize<Connection>(response)));
return tcs.Task;
}
我也试过这段代码,但仍然存在同样的问题。
代码2:
public async Task<Connection> Connect(string userId, string password)
{
var client = new RestClient(_baseUrl)
{
Authenticator = new SimpleAuthenticator("user", userId,
"password", password)
};
var tcs = new TaskCompletionSource<Connection>();
var request = new RestRequest(AppResources.Authenticating);
client.ExecuteAsync<Connection>(request, response => tcs.SetResult(new
JsonDeserializer().Deserialize<Connection>(response)));
Debug.WriteLine(tcs.Task.Result.Data);
return await tcs.Task;
}