我在 Android 的 xamarin 应用程序中遇到问题。 在我的主要 Activity 中,我调用了一个登录 Activity ,该 Activity 检查输入的用户和密码是否正确,它是通过 Microsoft.WindowsAzure.MobileServices 插件完成的。 我得到了答案,但是当我尝试在异步函数中打开一个新的 Activity 作为 Intent 时,应用程序挂起了结果。 我尝试从异步方法外部访问该目标 Activity 并且它有效。我的代码:
private async Task<int> GetAdminUser(string userName,string password)
{
int userId = -1;
try
{
users = await usersTable.Where (item => item.UserLoginName == userName && item.UserPassword == password).ToListAsync ();
if(users != null)
{
userId = users[0].UserId;
}
else{
userId = -1;
}
}
catch(Exception ex) {
userId = -1;
}
return userId;
}
private async Task GetAdminUserWrapper(string userName,string password)
{
int userId = await GetAdminUser (userName, password);
if (userId != -1)
{
//new AlertDialog.Builder (this).SetMessage ("User id : " + userId.ToString ()).Create().Show();
await OpenUserBusinessSalesIntent (userId);
}
}
async Task OpenUserBusinessSalesIntent (int userId)
{
try {
Intent testActivity = new Intent (this, typeof(TestActivity));
//userSalesActivity.AddFlags (ActivityFlags.NewTask);
//userSalesActivity.PutExtra ("UserIdBusiness",userId.ToString ());
//SetResult (Result.Ok,testActivity);
StartActivity (testActivity);
}
catch (Exception ex)
{
}
}
如有任何帮助,我们将不胜感激 什洛米
最佳答案
在 UI 线程上启动 Activity :
runOnUiThread(() => {
Intent testActivity = new Intent (this, typeof(TestActivity));
StartActivity (testActivity);
});
关于c# - 无法从异步函数启动新的 Intent Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24271044/