用于加载页面的wpf的进度条

时间:2013-07-26 14:38:40

标签: wpf wpf-controls wpftoolkit wpf-4.0

我的wpf应用程序中有两个wpf窗口。

1)当我点击加载按钮时,它会加载第二个窗口。 secong窗口需要15到20秒才能加载。

如何添加进度条以显示加载窗口以及第二个窗口何时加载关闭进度条。

3 个答案:

答案 0 :(得分:0)

有很多方法可以实现这一目标。一种简单的方法是使用进度条或等待动画创建第三个窗口或面板。第三个窗口负责加载第二个窗口,并在单击第一个窗口上的加载按钮后立即显示。第二个窗口的加载完成后,进度条的第三个窗口关闭,第二个窗口显示。

希望这会有所帮助。

答案 1 :(得分:0)

您可以将BusyIndi​​cator用作WPF扩展工具包的一部分。您可以在此处下载:http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator

在执行昂贵的处理之前立即加载新窗口,可以将IsBusy设置为true。处理完成后,将IsBusy设置为false。此方法涉及将XAML包装在第二个窗口中的BusyIndi​​cator中,这可能是您想要的也可能不是。

答案 2 :(得分:0)

我最近在我的应用程序的加载窗口上工作,点击该应用程序,加载大约需要10秒。我有一个带有中间装载杆的装载窗口。关键是将加载窗口放在不同的线程中以使动画在主线程上加载另一个窗口时运行。问题是要确保我们做出属性的东西(就像当我们关闭时我们关闭窗口应该停止线程...等)。

在下面的代码中...... 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();
                }));
        }
    }