c# - 无法从异步函数启动新的 Intent Activity

标签 c# android android-intent xamarin async-await

我在 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/

相关文章:

java - 嵌套 ScrollView 不滚动到 Activity 底部

android - AsyncTask 和 FragmentManager 的问题

android - 如何打开图库以选择多个图像?

javascript - 在 Android 的移动浏览器中向下滚动时,网站不会隐藏地址栏

android - 在 Android 中使用拨号盘启动应用程序

android - 向 Snapchat 发送 Intent

c# - DevExpress MVVM - 共享 ViewModel 和 EventToCommand

c# - Web.config 文件中是否需要程序集

c# - 如何根据时间和工资计算价格

c# - 线程池内存不足异常