WPF加载动画(WPF Loading animation)

编程入门 行业动态 更新时间:2024-10-26 18:15:23
WPF加载动画(WPF Loading animation)

我有一个ListView ,其中包含许多要在搜索中加载的项目。 我想为用户提供更丰富的用户界面,以便在加载时, 我会显示一个旋转圆圈(从AJAX等待中获知)

我意识到我必须进入Threads或其他东西,但是因为我以前从未在WPF中做过这些,所以我确信在WPF(或简单的BackgroundWorker)中有比Threads更好的东西。

无论如何,关键是在加载时显示该动画。 任何想法? 谢谢!

I Have a ListView with many Items that is to be loaded in search. And I'd like to provide user the richer user interface so that when loading, I'd display a rotating circle (known from AJAX waiting).

I realize that I'll have to go into Threads or something, but as I've never done this before in WPF, I'm sure there is something better than Threads in WPF (or a simpe BackgroundWorker).

Anyways, the point is to display that animation while loading. Any idea? Thanks!

最满意答案

好的,我现在有了一个有效的解决方案。

我有一个包含该动画的UserControl 。 它位于我的XAML代码中的某个位置,如下所示: <customControls:LoadingAnimation x:Name="LoadingAnimation" /> 。 通过调用neede加载此控件

LoadingAnimation.Show();

现在当我点击一个Button来完成耗时的工作时,在我调用BeginInvoke()之前,我加载了这个动画。

然后,当努力工作完成后,我调用LoadingAnimation.Hide(). 很简单! 我为其他人添加代码:

private void SearchClick(object sender, RoutedEventArgs e) { LoadingAnimation.Show(); new StringDelegate(DoSearch).BeginInvoke("TextToSearch", null, null); } private void DoSearch(string searchText) { object result = /* Do the time consuming work */ Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ResultDelagate(UpdateUserInterface), result); } private void UpdateUserInterface(object result) { LoadingAnimation.Hide(); DataContext = result as /* what you want */; }

OK I've got a working solution now.

I have a UserControl that contains that animation. It is located somewhere in my XAML code like this: <customControls:LoadingAnimation x:Name="LoadingAnimation" />. This control is loaded when neede by calling

LoadingAnimation.Show();

Now when I click on a Button to do the time-consuming work, before I call BeginInvoke() I load that animation.

Then when the hard work is finished I call LoadingAnimation.Hide(). Very simple! I add code for the others:

private void SearchClick(object sender, RoutedEventArgs e) { LoadingAnimation.Show(); new StringDelegate(DoSearch).BeginInvoke("TextToSearch", null, null); } private void DoSearch(string searchText) { object result = /* Do the time consuming work */ Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ResultDelagate(UpdateUserInterface), result); } private void UpdateUserInterface(object result) { LoadingAnimation.Hide(); DataContext = result as /* what you want */; }

更多推荐

Threads,WPF,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-08-03 16:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   动画   WPF   Loading   animation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!