GUI不会更新,直到功能执行结束

时间:2017-04-14 13:33:19

标签: android xamarin.android

我正在处理简单的登录活动。我有一个按钮和一个旋转动画。单击该按钮时,它应该运行动画,然后调用需要几秒钟才能完成的登录方法。

问题是当登录功能执行完成时(就在布局改变之前)动画开始。

这是我试过的:

 private void BtnpPrijava_Click(object sender, EventArgs e)
        {
            this.RunOnUiThread(() => {
            string username = edttxtKorisnicko.Text;
            string password = edttxtSifra.Text;

            var rotateAboutCornerAnimation = AnimationUtils.LoadAnimation(this, Resource.Layout.RotationAnimation);
            imageViewForRotation.Visibility = ViewStates.Visible;
            imageViewForRotation.StartAnimation(rotateAboutCornerAnimation);

            success = LogInController.Instance.Login(username, password);    

            if (success)
            {
                StartActivity(typeof(MainActivity));
            }
        }}

我还尝试在RunOnUIThread中只放置这3行代码:

    var rotateAboutCornerAnimation = AnimationUtils.LoadAnimation(this, Resource.Layout.RotationAnimation);
    imageViewForRotation.Visibility = ViewStates.Visible;
    imageViewForRotation.StartAnimation(rotateAboutCornerAnimation);

有人能指出我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

解决方案是将所有代码放在RunOnUiThread中的新线程和上面提到的3行中。

    private void BtnpPrijava_Click(object sender, EventArgs e)
    {
        new System.Threading.Thread(new System.Threading.ThreadStart(() =>
        {
            string korisnicko = edttxtKorisnicko.Text;
            string sifra = edttxtSifra.Text;

            if (korisnicko != "" && sifra != "")
            {
                bool prijavljen = false;


                var rotateAboutCornerAnimation = AnimationUtils.LoadAnimation(this, Resource.Layout.RotationAnimation);

                this.RunOnUiThread(() =>
                {
                    ivRotacija.Visibility = ViewStates.Visible;
                    ivRotacija.StartAnimation(rotateAboutCornerAnimation);
                });


                prijavljen = LogInController.Instance.Prijava(korisnicko, sifra);


                if (prijavljen)
                {
                    StartActivity(typeof(MainActivity));
                }
                else
                {
                    ivRotacija.Visibility = ViewStates.Visible;
                }
            }
        })).Start();
    }