Ivan D'souza
Ivan D'souza

Reputation: 105

How to use busy indicator in wpf

My page within a frame takes to time to load meaning the controls take some time to appear on the page for the first time. Where in my main window.cs file should I set the IsBusy = true.I have no idea how to use busy indicator.When should I switch it to true or false. please guide me how should I use it ? Thanks in advance.

Upvotes: 2

Views: 8685

Answers (2)

Eldho
Eldho

Reputation: 8273

Wrap you Xaml with a busy indicator . Assuming you are using MVVM

  <xctk:BusyIndicator BusyContent="{Binding BusyText}" IsBusy="{Binding IsBusy}">
    <Grid>
       <!--Your controls and content here-->
    </Grid>
</xctk:BusyIndicator>

In your viewmodel

    /// <summary>
    /// To handle the Busy Indicator's state to busy or not
    /// </summary>
    private bool _isBusy;
    public bool IsBusy
    {
        get
        {
            return _isBusy;
        }
        set
        {
            _isBusy = value;
            RaisePropertyChanged(() => IsBusy);
        }
    }

    private string _busyText;
    //Busy Text Content
    public string BusyText
    {
        get { return _busyText; }
        set
        {
            _busyText = value;
            RaisePropertyChanged(() => BusyText);
        }
    }

Command and Command Handler

    //A Command action that can bind to a button
    private RelayCommand _myCommand;
    public RelayCommand MyCommand
    {
        get
        {
            return _myCommand??
                   (_myCommand= new RelayCommand(async () => await CommandHandler(), CanExecuteBoolean));
        }
    }

internal async Task CommandHandler()
    {
       Isbusy = true;
       BusyText = "Loading Something...";
       Thread.Sleep(3000); // Do your operation over here
       Isbusy = false;
    }

Upvotes: 2

netniV
netniV

Reputation: 2418

Generally you would set the busy indicator before you start doing a load of heavy processing so that is dependant on your code.

It would normally before just before you spawn a background thread to do a load of work leaving the UI to say it's busy at the moment and when the thread is completing then "unbusy" the UI.

Upvotes: 3

Related Questions