question

marcincz avatar image
0 Votes"
marcincz asked LeonLu-MSFT edited

No change in visibility when changing the flag with binding

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"/>
     (...)






dotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT edited

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.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@marcincz Did you try to use above code? If it is still not work, you could share a demo about it.

0 Votes 0 ·