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

WPF在单独的线程中运行动画,wpf,multithreading,data-binding,Wpf,Multithreading,Data Binding,我有一个应用程序,我必须绑定一个数据网格,它位于选项卡项中的用户控件上 这个过程需要一段时间,所以我在一个单独的线程中创建另一个带有加载动画的选项卡项,当我在数据网格中加载数据时,我选择带有动画的选项卡项,直到加载数据为止 问题是动画正在启动,但当数据网格绑定启动时,动画将冻结 this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action( delegate()

我有一个应用程序,我必须绑定一个数据网格,它位于选项卡项中的用户控件上

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

问题是动画正在启动,但当数据网格绑定启动时,动画将冻结

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);

对话框启动绑定后,加载环境冻结。

这是可以理解的。如果您有一个长期运行的进程,您应该使用它来运行它

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);
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.
}
把你的动画放在不同的标签上是不是很好看?您是否考虑过使用来自的
BusyIndicator
?您可以简单地使用BusyIndicator包装控件,并使用其IsBusy属性来运行它



谢谢,我将尝试使用BusyIndicator,但我不能使用BackgroundWorker,因为它必须是STA线程。@user2039717我不明白为什么。。。您不应该在
BackgroundRoker
中制作动画。您应该在后台为DataGrid加载这些项。。。没有理由认为它应该在STA线程上。我认为你从错误的角度看待你的问题。