c# – 使用UserControl和ViewModel的WPF中的BusyIndi​​cator

繁忙指示似乎不起作用.我在加载数据之前将IsBusy标志设置为true,并在加载数据完成后将其设置为false

但指标没有出现.以下是我的代码snippit.

<UserControl ...
xmlns:WPFTool="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit">

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding FormLoadCompleteCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

 <Grid>

    .... 

    <WPFTool:BusyIndicator Name="BusyInd" BusyContent="Loading Servers" IsBusy="{Binding IsBusy}"/>
 </Grid>
</UserControl>


public class MyViewModelForUserControl : ViewModelBase
{

    private bool _isBusy;
    public bool IsBusy { get { return _isBusy; } set { _isBusy = value; RaisePropertyChanged("IsBusy"); } }


    private DelegateCommand _formLoadCompleteCommand;
    public DelegateCommand FormLoadCompleteCommand
    {
        get
        {
            if (_formLoadCompleteCommand == null)
                _formLoadCompleteCommand = new DelegateCommand(FormLoadComplete);
            return _formLoadCompleteCommand;
        }
    }


    private void FormLoadComplete(object parameter)
    {   
        IsBusy = true;
        LoadData();
        IsBusy = false;
    }
}

最佳答案 试试这个:

    private void FormLoadComplete(object parameter)
    {
        IsBusy = true;
        Task.Run(() =>
        {
            LoadData();
            Application.Current.Dispatcher.Invoke(() => IsBusy = false);
        });
    }

您必须保留第一个事件处理程序并且不在UI线程上运行LoadData.

点赞