简体   繁体   English

WPF的进度条以加载页面

[英]Progressbar for a wpf to load a page

I have two wpf windows in my wpf application. 我的wpf应用程序中有两个wpf窗口。

1) When i click on the load button it loads the second Window. 1)当我点击加载按钮时,它加载第二个窗口。 The secong window takes 15 to 20 secs to load. secong窗口需要15到20秒才能加载。

How do i add a progress bar to show a loading window and when the second window loads close the progress bar. 如何添加进度条以显示加载窗口,以及在第二个窗口加载时关闭进度条。

There are many ways you can accomplish that. 您可以通过多种方式来实现这一目标。 An easy way would be to create a third window or panel with the progress bar or a waiting animation. 一种简单的方法是使用进度条或等待的动画创建第三个窗口或面板。 That third window is in charge of loading your second window and get displayed as soon as you click the load button on the first window. 第三个窗口负责加载第二个窗口,并在单击第一个窗口上的加载按钮后立即显示该窗口。 When loading of the second window is completed, the third window with the progress bar closes and the second window gets displayed. 当第二个窗口的加载完成时,带有进度栏的第三个窗口将关闭,并显示第二个窗口。

hope this helps. 希望这可以帮助。

You could use the BusyIndicator as part of the WPF extended toolkit. 您可以将BusyIndi​​cator用作WPF扩展工具包的一部分。 You can download it here: http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator 您可以在这里下载: http : //wpftoolkit.codeplex.com/wikipage?title=BusyIndi​​cator

On immediate loading of the new window before you do your expensive processing that takes forever, you can set the IsBusy to true. 在立即执行新操作之前,立即加载新窗口时,可以将IsBusy设置为true。 When processing is done, set IsBusy back to false. 处理完成后,将IsBusy设置为false。 This method involves wrapping your XAML in the BusyIndicator in the 2nd window, which may or may not be what you want. 此方法涉及将XAML包装在第二个窗口的BusyIndi​​cator中,这可能是您想要的,也可能不是。

I was recently working on a loading window for my app where you click the app and it takes about 10s to load. 我最近正在为我的应用程序加载窗口,在该窗口中单击该应用程序,加载该过程大约需要10秒钟。 I have a loading window with an intermediate loading bar. 我有一个带有中间加载栏的加载窗口。 The key was to put the loading window in a different thread to enable the animation to run while it was loading the other window on the main thread. 关键是将加载窗口放置在其他线程中,以使动画在将另一个窗口加载到主线程时运行。 The problem was to make sure we do stuff propertly (like when we close we close the window should stop the thread... etc). 问题是要确保我们做得正确(例如,当我们关闭窗口时,关闭窗口应该停止线程...等等)。

In the code below... LoadingWindow is a small window with a progress bar on it, SecondWindow would be the window that is slow to load. 在下面的代码中... LoadingWindow是一个带有进度条的小窗口, SecondWindow将是加载缓慢的窗口。

    public void OnLoad()
    {
        Dispatcher threadDispacher = null;

        Thread thread = new Thread((ThreadStart)delegate
        {
            threadDispacher = Dispatcher.CurrentDispatcher;
            SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(threadDispacher));

            loadingWindow = new LoadingWindow();
            loadingWindow.Closed += (s, ev) => threadDispacher.BeginInvokeShutdown(DispatcherPriority.Background);
            loadingWindow.Show();

            System.Windows.Threading.Dispatcher.Run();
        });

        thread.SetApartmentState(ApartmentState.STA);
        thread.IsBackground = true;
        thread.Start();

        // Load your second window here on the normal thread
        SecondWindow secondWindow = new SecondWindow();

        // Presumably a slow loading task            

        secondWindow.Show();

        if (threadDispacher != null)
        {
            threadDispacher.BeginInvoke(new Action(delegate
                {
                    loadingWindow.Close();
                }));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM