【发布时间】:2013-07-26 14:38:39
【问题描述】:
我的 wpf 应用程序中有两个 wpf 窗口。
1) 当我点击加载按钮时,它会加载第二个窗口。 secong 窗口需要 15 到 20 秒才能加载。
如何添加进度条以显示加载窗口以及在第二个窗口加载时关闭进度条。
【问题讨论】:
标签: wpf wpf-controls wpftoolkit wpf-4.0
我的 wpf 应用程序中有两个 wpf 窗口。
1) 当我点击加载按钮时,它会加载第二个窗口。 secong 窗口需要 15 到 20 秒才能加载。
如何添加进度条以显示加载窗口以及在第二个窗口加载时关闭进度条。
【问题讨论】:
标签: wpf wpf-controls wpftoolkit wpf-4.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();
}));
}
}
【讨论】:
loadingWindow 不想在我的应用程序加载完成时关闭,我必须手动关闭它或让 progressBar 不确定并设置为当 progressBar 的值达到 100 时 loadingWindow 关闭。
有很多方法可以做到这一点。一种简单的方法是创建带有进度条或等待动画的第三个窗口或面板。第三个窗口负责加载您的第二个窗口,并在您单击第一个窗口上的加载按钮后立即显示。当第二个窗口加载完成后,带有进度条的第三个窗口将关闭并显示第二个窗口。
希望这会有所帮助。
【讨论】:
您可以将 BusyIndicator 用作 WPF 扩展工具包的一部分。你可以在这里下载:http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator
在您执行昂贵且耗时的处理之前立即加载新窗口时,您可以将 IsBusy 设置为 true。处理完成后,将 IsBusy 设置回 false。此方法涉及将您的 XAML 包装在第二个窗口的 BusyIndicator 中,这可能是您想要的,也可能不是。
【讨论】: