Simple Activity Indicator

Hi,

i just want to show the activity indicator, run my method and hide the activity indicator clicking a button. Should be very simple, shouldn't it?
But it does not Show the activity indicator. Need to use async/await, but don't know exactly how...

XAML:
<ActivityIndicator x:Name="Ain_load" IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" Color="Gray" BackgroundColor="Transparent" Scale="3" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" HorizontalOptions="Center" VerticalOptions="Center"/>

.cs:
` private bool _isBusy;
public bool IsBusy
{
get { return _isBusy; }
set
{
_isBusy = value;
OnPropertyChanged();
}
}

    private void Btn_Wurf_Clicked(object sender, EventArgs args)
    {

        IsBusy = true;

    //Do its work

   IsBusy =false;
 }`
Tagged:

Best Answer

Answers

  • exendahalexendahal Member ✭✭

    ..

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Do you have the BindingContext of the page set to this?

  • MamagotchiMamagotchi Member ✭✭

    @JoeManke said:
    Do you have the BindingContext of the page set to this?

    nope, thx. But I don't know really How... I'm really sorry.

    XAML:
    `

    <ContentPage.BindingContext>
        <x:Reference Name="This" />
    </ContentPage.BindingContext>
    

    //rest of it`

    It's not working.

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    In your page's constructor in the .xaml.cs file: BindingContext = this;

  • igorkr_10igorkr_10 Member ✭✭

    You need to know about MVVM . After reading this article create your BaseViewModel (you will inherit other viewmodels from it in future):

    public abstract class ViewModel : INotifyPropertyChanged
    {
        bool isBusy = false;
        public bool IsBusy
        {
            get { return isBusy; }
            set { SetProperty(ref isBusy, value); }
        }
    
        protected bool SetProperty<T>(ref T backingStore, T value, [CallerMemberName]string propertyName = "", Action onChanged = null)
        {
            if (EqualityComparer<T>.Default.Equals(backingStore, value))
                return false;
            backingStore = value;
            onChanged?.Invoke();
            OnPropertyChanged(propertyName);
            return true;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            var changed = PropertyChanged;
            if (changed == null)
                return;
            changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    Then create your page, somewhere in xaml put indicator:

    <ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" Color="Orange"/>

    Don`t forget button

    <Button Text="Button" Clicked="ButtonClicked"/>

    In your page code do this:

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class YourPage : ContentPage
    {
        private YourViewModel _viewModel;
    
        public YourPage ()
        {
            InitializeComponent ();
                this.BindingContext = _viewModel = new YourViewModel();
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
        }
    
        private async void ButtonClicked(object sender, EventArgs e)
        {
            try
            {
                _viewModel.IsBusy = true;
                await _viewModel.SomeLongRunningMethod()
            }
            catch(Exception ex)
            {
                await DisplayAlert("Error", ex.Message, "OK");
            }
            finally
            {
                _viewModel.IsBusy = false;
            }
        }       
    }
    

    Then your need to create inherited viewmodel from base:

    public class YourViewModel : ViewModel
    {
        public async Task SomeLongRunningMethod()
        {
            await Task.Factory.StartNew(()=>Task.Delay(1000));
        }
    }
    
Sign In or Register to comment.