WPF中的BusyIndi​​cator使用UserControl和ViewModel

时间:2017-03-15 09:17:55

标签: c# wpf

忙碌指示似乎不起作用。我在加载数据之前将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;
    }
}

1 个答案:

答案 0 :(得分:2)

试试这个:

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

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