Triggering Activity Indicator in Xamarin Forms

后端 未结 2 1060
南旧
南旧 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:35

    James' answer is correct, however, I prefer to use the page's own IsBusy property, for two reasons:

    1. I'm lazy and I don't want to set-up INotifyPropertyChanged implementation if I don't have to
    2. XF is suppossed to use that property to display a notification by default. This doesn't appear to be working, but if it ever does, this approach will already have us covered to take advantage of it

    The only difference with James' answer (besides deleting the INPC implementation) is that you need to give the page a name (x:Name="myPage") and then declare the binding using a reference to it using {Binding Source={x:Reference myPage}, Path=IsBusy} for the ActivityIndicator's IsVisible and IsRunning values.

    i.e.:

    MainPage.xaml:

    
        ...
        
        ...
    
    

    MainPage.xaml.cs:

    ...
    async void OnDoSomethingLong(...)
    {
        if (!this.IsBusy)
        {
            try
            {
                this.IsBusy = true;
    
                //await long operation here, i.e.:
                await Task.Run(() => {/*your long code*/});
            }
            finally
            {
                this.IsBusy = false;
            }
        }
    }
    ...
    

提交回复
热议问题