No change in visibility when changing the flag with binding

Marcin 21 Reputation points
2021-03-23T22:41:37.98+00:00

I am having trouble setting the visibility of the ActivityIndicator.
When IsBusy = true in view model, the indicator is not visible in the view. How to solve it?

sample code in view model (MyViewModel):

 private bool _IsBusy;
    public bool IsBusy
    {
        get { return _IsBusy; }
        set { SetProperty(ref _IsBusy, value); }
    }

void MyDataChanged(object sender, ObjectChangeEventArgs e)
{            
    this.IsBusy = true;
    CommitChanges(); //takes aprox. 3 seconds
    this.IsBusy = false; 
}

My xaml in View:

    (...)
    xmlns:viewmodels="clr-namespace:Project.ViewModels" 
    x:DataType="viewmodels:MyViewModel"
    (...)
    <ActivityIndicator Grid.Row="1" Grid.Column="0" HeightRequest="100" WidthRequest="100" HorizontalOptions="Center" IsVisible="{Binding IsBusy}" IsRunning="True"/>
    (...)
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,261 Reputation points Microsoft Vendor
    2021-03-24T01:51:07.667+00:00

    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.


0 additional answers

Sort by: Most helpful