在窗口加载 wpf 窗口时显示进度条?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20241970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:07:49  来源:igfitidea点击:

Showing progress bar while window is loading with wpf window?

c#wpf

提问by YoMo

I have window with a lot of items and tools, what I want to do, and ask about, is how can I show a progress bar while the window is loading with a WPF main or child window?

我有很多项目和工具的窗口,我想要做什么,并询问我如何在窗口加载 WPF 主窗口或子窗口时显示进度条?

回答by HDD

You could try this link:

你可以试试这个链接:

http://bensprogrammingwork.blogspot.com.br/2012/01/progressbar-in-wpf.html

http://bensprogrammingwork.blogspot.com.br/2012/01/progressbar-in-wpf.html

Might be helpful, I could try to explain more but I don't really know what's your struggle.

可能会有所帮助,我可以尝试解释更多,但我真的不知道您的挣扎是什么。

回答by Sheridan

It is pointless attempting to do that with WPF because by the time the Frameworkhas loaded your application and you are able to display a Windowwith a progress bar, the MainWindow.xamlpage will have already loaded.

尝试使用 WPF 执行此操作是毫无意义的,因为当Framework您的应用程序加载完毕并且您能够显示Window带有进度条的 时,MainWindow.xaml页面将已经加载。

There is a way to display a static image beforethe application has fully loaded, but you cannot animate any part of it. You can find out how to do that in the How to: Add a Splash Screen to a WPF Applicationpage on MSDN.

有一种方法可以在应用程序完全加载之前显示静态图像,但您不能为其任何部分设置动画。您可以在 MSDN 上的如何:将启动画面添加到 WPF 应用程序页面中了解如何执行此操作。

If you definitely want a loading animation upon start up, you will have to wait until the application has finished loading and then 'pretend' to the users that it is still loading with your animation... pretty pointless I think you would agree.

如果您确实想要在启动时加载动画,则必须等到应用程序完成加载,然后向用户“假装”它仍在加载您的动画……我认为您会同意这一点毫无意义。

Even so, if you are determined to go down that path, you can find a working example of a loading animation in the A Simple WPF Loading Animationpage on the ElegantCode website.

即便如此,如果您决定沿着这条路走下去,您可以在 ElegantCode 网站上的简单 WPF 加载动画页面中找到加载动画的工作示例。



UPDATE >>>

更新 >>>

Oh sorry, I didn't realise that this was not just for the start up Window. No, you can only use the splash screen upon application start up. The problem that you will find when displaying an animation when a Windowis loading is that (generally) the Windowis loaded using the UI thread. As your animation will also be using the UI thread, you will find that it probably won't run smoothly because the UI thread is busy loading and initialising the Window. You can still try using the last link that I provided for this, but don't expect too much.

哦对不起,我没有意识到这不仅仅是为了启动Window。不可以,您只能在应用程序启动时使用启动画面。在Window加载a 时显示动画时会发现的问题是(通常)Window是使用 UI 线程加载的。由于您的动画也将使用 UI 线程,您会发现它可能不会顺利运行,因为 UI 线程正忙于加载和初始化Window. 您仍然可以尝试使用我为此提供的最后一个链接,但不要期望太多。