C# 无法从异步函数启动新的意图活动

C# 无法从异步函数启动新的意图活动,c#,android,android-intent,xamarin,async-await,C#,Android,Android Intent,Xamarin,Async Await,我在android的xamarin应用程序中遇到了一个问题。 在我的主要活动中,我调用了一个登录活动来检查输入的用户和密码是否正确,这是通过Microsoft.WindowsAzure.MobileServices插件完成的。 我得到了答案,但当我试图在异步函数中打开一个新的活动作为意图,并得到结果时,应用程序挂起。 我尝试从异步方法外部访问该目标活动,结果它成功了。我的代码: private async Task<int> GetAdminUser(string userName,

我在android的xamarin应用程序中遇到了一个问题。 在我的主要活动中,我调用了一个登录活动来检查输入的用户和密码是否正确,这是通过Microsoft.WindowsAzure.MobileServices插件完成的。 我得到了答案,但当我试图在异步函数中打开一个新的活动作为意图,并得到结果时,应用程序挂起。 我尝试从异步方法外部访问该目标活动,结果它成功了。我的代码:

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) 
            {

            }
        } 
私有异步任务GetAdminUser(字符串用户名、字符串密码)
{
int userId=-1;
尝试
{
users=wait usersTable.Where(item=>item.UserLoginName==userName&&item.UserPassword==password);
如果(用户!=null)
{
userId=users[0]。userId;
}
否则{
userId=-1;
}
}
捕获(例外情况除外){
userId=-1;
}
返回用户标识;
}
专用异步任务GetAdminUserWrapper(字符串用户名、字符串密码)
{
int userId=await GetAdminUser(用户名、密码);
如果(用户ID!=-1)
{
//新建AlertDialog.Builder(this.SetMessage)(“用户id:+userId.ToString()).Create().Show();
等待OpenUserBusinessSalesIntent(用户ID);
}
}
异步任务OpenUserBusinessSalesIntent(int userId)
{
试一试{
意向测试活动=新意向(此,类型为(测试活动));
//userSalesActivity.AddFlags(ActivityFlags.NewTask);
//userSalesActivity.PutExtra(“UserIdBusiness”,userId.ToString());
//SetResult(Result.Ok,testActivity);
StartActivity(测试性);
} 
捕获(例外情况除外)
{
}
} 
任何帮助都将受到感谢
Shlomy

在UI线程上启动活动:

runOnUiThread(() => {
  Intent testActivity = new Intent (this, typeof(TestActivity));

  StartActivity (testActivity);
});

感谢您的想法,崩溃发生在我尝试在异步函数中启动新意图时 在我得到用户ID之后。 解决方案是使用RunOnUiThread并用try catch将其包装在RunOnUiThread内 开始意图。 10倍
Shlomy

哪种方法挂起?而且很难理解你上一种方法到底发生了什么。如果您不等待任何内容,为什么会将其标记为
async
?是否在任何
任务
上调用
结果
等待
?活动是一个UI组件,应该在主UI线程上运行。非UI线程上的UI操作可能会导致各种问题,包括挂起/崩溃应用程序不应该引发异常?