首页 > 解决方案 > 如何在登录时或其他活动之间显示加载或 IsBusy 图标

问题描述

我想(IsBusy)在登录应用程序时显示加载图标以有效显示,并在成功登录后消失。就我而言,它根本没有发生。

在该finally步骤中,如果设置为IsBusy = true,那么我可以看到正在显示(为此添加了屏幕截图),但这不是我想要的。我希望它在登录应用程序时显示一小部分时间,并且它应该在成功登录后消失一次。

注意:对于我设置的所有其他条件IsBusy=false

<ActivityIndicator x:Name="activityIndicator" IsRunning="{Binding IsBusy}" VerticalOptions="Center" HorizontalOptions="Center"></ActivityIndicator>

public MainPage()
    {
       InitializeComponent();
       this.BindingContext = this;
       this.IsBusy = false;
    }

public async void Button_Clicked(object sender, System.EventArgs e)
        {
            this.IsBusy = true;
            string emailText = emailEntry.Text;
            string passwordText= passwordEntry.Text; 
            if(!string.IsNullOrWhiteSpace(emailEntry.Text) && !string.IsNullOrWhiteSpace(passwordEntry.Text))
            {
                if(ValidateEmail(emailText) == true)
                {
                    int count = (from x in conn.Table<PlayerDetails>().Where(x => x.Email == emailText) select x).Count();
                    if (count!= 0)
                    {
                        try
                        {
                           List<PlayerDetails> myList = (from x in conn.Table<PlayerDetails>().Where(x => x.Email == emailText && x.Password == passwordText) select x).ToList();
                            if(myList.Count() > 0)
                            {
                              // do the login stuff here.....
                            }
                            else
                            {
                                this.IsBusy = false;
                                await DisplayAlert("Notification", "No such email or password", "Cancel");

                            }

                        }
                        catch (NullReferenceException ex)
                        {
                          if(ex!=null)
                            Debug.WriteLine("Something is thrown ! " + e.ToString());
                        }
                        finally
                        {
                            IsBusy = false;
                        }
                    }
                    else
                    {
                        this.IsBusy = false;
                        await DisplayAlert("Notification", "Unable to find the user details", "Cancel");
                    } 
                }
                else
                {
                    this.IsBusy = false;
                    await DisplayAlert("Notification", "Email is not a valid one", "Cancel");
                }   
            }
            else
            {
                this.IsBusy = false;
                await DisplayAlert("Notification","Input details cannot be blank", "Cancel");
            }
        }

在此处输入图像描述

标签: xamarinxamarin.forms

解决方案


我刚刚测试了你的代码,我发现它有效。

看不到 的原因ActivityIndicator是代码执行得非常快,甚至看不到ActivityIndicatorrunning变为的过程not running

如果你在设置后加一点延迟IsBusy = true,你会发现它确实有效:

 this.IsBusy = true;
 Task.Delay(TimeSpan.FromMilliseconds(2000));    
 string emailText = emailEntry.Text;
 string passwordText= passwordEntry.Text; 

推荐阅读