WPF在单独的线程中运行动画

时间:2013-02-04 13:33:07

标签: wpf multithreading data-binding

我有一个应用程序,我必须将一个数据网格绑定在一个标签项中的用户控件上。

这个过程需要一段时间,所以我在一个单独的线程中创建另一个带有加载动画的Tab项目,当我在数据网格中加载数据时,我选择带有动画的标签项,直到加载数据。

问题是动画正在启动,但是当数据网格绑定开始时,动画就会冻结。

this.Dispatcher.Invoke(DispatcherPriority.Normal,
            new Action(
                delegate()
                {
                    tiLoading.Content = new Controls.Loading.LoadingAnimation();
                    tiLoading.IsSelected = true;
                }
        ));

//And now I populate the content of the Tab Item with the User Control that contains data grid

connType = choseConnType(); // Here is a dialog witch ask the user somethig and return a value
tiGeneral.Content = new Windows.General(true, connType);

在开始绑定对话框后,LoadingAnimation正在冻结。

1 个答案:

答案 0 :(得分:0)

这是可以理解的。如果您有长时间运行的进程,则应使用BackgroundWorker来运行它。

public void ItsGonnaBeLong()
{
    this.IsBusy = true;
    var worker = new BackgroundWorker();
    worker.DoWork += this.MyDoWorkMethod;
    worker.RunWorkerCompleted += this.MyDoWorkCompleted;
    worker.RunWorkerAsync();
}

private void MyDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{
   // executed after work on background thread is completed
   this.IsBusy = false;
}

private void MyDoWorkMethod(object sender, DoWorkEventArgs e)
{
   // Do something.
}

将动画放在不同的TabItem上是否很好?您是否考虑过使用Extended WPF Toolkit中的BusyIndicator?您可以使用BusyIndi​​cator简单地包装Control并使用其IsBusy属性来运行它。

<xctk:BusyIndicator IsBusy="True" >
        <my:MyUserControl />
</xctk:BusyIndicator>
相关问题