如何在wpf中使用忙碌指示器

How to use busy indicator in wpf

框架内的我的页面需要一些时间来加载,这意味着控件需要一些时间才能首次出现在页面上。我应该在主 window.cs 文件中的哪个位置设置 IsBusy = true.I 不知道如何使用 busy indicator.When 应该将其切换为 true 还是 false。请指导我应该如何使用它?提前致谢。

通常,您会在开始进行大量处理之前设置忙碌指示器,这取决于您的代码。

通常会在您生成后台线程做大量工作之前离开 UI 说它现在很忙,当线程完成时 "unbusy" UI。

用繁忙的指示器包裹你 Xaml。假设您使用 MVVM

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

在你的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);
        }
    }

命令和命令处理程序

    //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;
    }