Hello,
Welcome to our Microsoft Q&A platform!
You should move this.IsBusy = false;
to the end of CommitChanges
method. And I notice you used MVVM. I make a test with Button's Command
. Here is MyViewModel
public class MyViewModel: BaseViewModel
{
private bool _IsBusy;
public bool IsBusy
{
get { return _IsBusy; }
set { SetProperty(ref _IsBusy, value); }
}
public ICommand TestCommand { protected set; get; }
public MyViewModel()
{
TestCommand = new Command(async (key) =>
{
IsBusy = true;
CommitChanges(); //takes aprox. 3 seconds
// IsBusy = false;
});
}
private async void CommitChanges()
{
await Task.Delay(3000);
IsBusy = false;
}
}
public abstract class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(
[CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(
this, new PropertyChangedEventArgs(propertyName));
}
protected void SetProperty<T>(ref T backingField,
T value,
[CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(
backingField, value)) return;
backingField = value;
OnPropertyChanged(propertyName);
}
}
}
Here is my xaml.
<StackLayout>
<Button Text="exe" Command="{Binding TestCommand}"></Button>
<ActivityIndicator Grid.Row="1" Grid.Column="0" HeightRequest="100" WidthRequest="100" HorizontalOptions="Center" IsVisible="{Binding IsBusy}" IsRunning="True"/>
</StackLayout>
Here is my layout's background code.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.BindingContext = new MyViewModel();
}
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.