Triggering Activity Indicator in Xamarin Forms

后端 未结 2 1041
南旧
南旧 2021-01-05 17:49

I have an icon on my Xamarin.Forms app, which when it is clicked, I would like to change it to an activity indicator. It looks as though I should use a Trigger, Event Trigge

2条回答
  •  礼貌的吻别
    2021-01-05 18:42

    There are a few ways of doing this.

    You could simply give each of them an x:Name and then turn on/off IsRunning and IsVisible if you want to hide it.

    I assume though that you have some data binding going on. Since IsRunning is a bool you could simply bind it to a boolean in your code behind. For instance In my ViewModel I have an IsBusy property and implement INotifyPropertyChanged:

    public class MyViewModel : INotifyPropertyChanged
    {
    
    
        public MyViewModel()
        {
        }
    
        private bool busy = false;
    
        public bool IsBusy
        {
            get { return busy; }
            set
            {
                if (busy == value)
                    return;
    
                busy = value;
                OnPropertyChanged("IsBusy");
            }
        }
    
    
        public async Task GetMonkeysAsync()
        {
            if (IsBusy)
                return;
    
    
            try
            {
                IsBusy = true;
    
                //do stuff here that is going to take a while
    
            }
            finally
            {
                IsBusy = false;
            }
        }
    
        #region INotifyPropertyChanged implementation
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void OnPropertyChanged(string name)
        {
            var changed = PropertyChanged;
            if (changed == null)
                return;
    
            changed(this, new PropertyChangedEventArgs(name));
    
        }
    
        #endregion
    }
    

    Then in the XAML you can bind to IsBusy:

    
    

    I think that should handle it. If you need a timer you could use the same binding that I have here and use Xamarin.Forms built in timer class.

提交回复
热议问题