ProgressBar可见性被忽略

时间:2018-10-18 23:26:10

标签: c# asynchronous xamarin.android

我试图在启动异步任务时显示进度条,并且在异步任务运行完毕后,将进度条设置回不可见。由于某种原因,progressBar无法响应更改可见性属性的代码。

        Activity.RunOnUiThread(() =>
        {
            prog.Visibility = ViewStates.Visible;
        });

        Task<bool> createPickTask = Task.Run(() => Utils.createPick(firstBet, myBet, game));
                bool createPickResult = createPickTask.Result;

        if (createPickResult)
        {
            adapter.NotifyItemChanged(pos);
        }
        else
        {
            showErrorMessage();
        }

        Activity.RunOnUiThread(() =>
        {
            prog.Visibility = ViewStates.Gone;
        });

1 个答案:

答案 0 :(得分:2)

  

由于某种原因,progressBar无法响应更改可见性属性的代码。

直到这部分代码全部执行后,代码才会在UI上生效。 您可以尝试在“任务”中设置进度条的可见性。例如:

    private void Button_Click(object sender, System.EventArgs e)
    {
        Activity.RunOnUiThread(() =>
        {
            prog.Visibility = ViewStates.Visible;
        });

        Task<bool> createPickTask = Task.Run(/*async*/ () => {
            //await Task.Delay(3000);
            bool createPickResult = Utils.createPick(firstBet, myBet, game);

            if (createPickResult)
            {
                adapter.NotifyItemChanged(pos);
            }
            else
            {
                showErrorMessage();
            }

            Activity.RunOnUiThread(() =>
            {
                prog.Visibility = ViewStates.Gone;
            });
            return createPickResult;
        });
    }

结果是:

enter image description here