WPF在单独的线程中运行动画 [英] WPF Run Animation in a separate Thread
问题描述
这个过程需要一段时间才能创建另一个在单独的线程中加载动画并在数据网格中加载数据的Tab项目我选择具有动画的选项卡项,直到数据加载为止。
问题是动画开始,但是当数据网格绑定开始时,动画已冻结。
this.Dispatcher.Invoke(DispatcherPriority .Normal,
new Action(
delegate()
{
tiLoading.Content = new Controls.Loading.LoadingAnimation();
tiLoading.IsSelected = true;
}
));
//现在我用包含数据网格的用户控件填充Tab Item的内容
connType = chosenConnType(); //这里是一个对话框,向用户询问某个值并返回一个值
tiGeneral.Content = new Windows.General(true,connType);
启动绑定后的对话框,LoadAnimation正在冻结。
这是可以理解的。如果您有长期运行的过程,您应该使用 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)
{
//在后台线程完成后执行
this.IsBusy =假;
}
private void MyDoWorkMethod(object sender,DoWorkEventArgs e)
{
//做某事。
}
是否可以将动画放在不同的TabItem上?您是否考虑使用扩展WPF工具包中的 BusyIndicator
?您可以使用BusyIndicator简单地包装控件,并使用它的IsBusy属性来运行它。
xctk:BusyIndicator IsBusy =True>
< my:MyUserControl />
< / xctk:BusyIndicator>
I have an application where I have to bind a Data Grid witch is on an User control in a Tab Item.
This process take a while so I create another Tab Item with a Loading animation in a separate thread and when I load data in data grid I select the tab item with the animation until the data is loaded.
The problem is that the animation is starting, but when the data grid binding starts, the animation is freezing.
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);
After the dialog when starts the binding the LoadingAnimation is freezing.
It's understandable. If you have long-running process you should be using BackgroundWorker to run it.
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.
}
And is it pretty to have your animation on different TabItem? Have you considered using BusyIndicator
from Extended WPF Toolkit? You can simply wrap your Control with BusyIndicator and use Its IsBusy property to run it.
<xctk:BusyIndicator IsBusy="True" >
<my:MyUserControl />
</xctk:BusyIndicator>
这篇关于WPF在单独的线程中运行动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!