gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:13:11 24 4
gpt4 key购买 nike

我有一个应用程序,我必须在选项卡项中的用户控件上绑定(bind)一个数据网格女巫。

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

问题是动画正在开始,但是当数据网格绑定(bind)开始时,动画是卡住的。

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

在开始绑定(bind)的对话框之后,LoadingAnimation 被卡住。

最佳答案

这是可以理解的。如果你有长时间运行的进程,你应该使用 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 上是不是很漂亮?您是否考虑过使用 BusyIndicator来自 Extended WPF Toolkit ?您可以简单地用 BusyIndi​​cator 包装您的控件并使用它的 IsBusy 属性来运行它。

<xctk:BusyIndicator IsBusy="True" >
<my:MyUserControl />
</xctk:BusyIndicator>

关于WPF 在单独的线程中运行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14687870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com