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
James' answer is correct, however, I prefer to use the page's own IsBusy
property, for two reasons:
INotifyPropertyChanged
implementation if I don't have toThe 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;
}
}
}
...