我的 wpf 应用程序中有两个 wpf 窗口。
1)当我点击加载按钮时,它会加载第二个窗口。secong 窗口需要 15 到 20 秒才能加载。
如何添加进度条以显示加载窗口以及第二个窗口加载时关闭进度条。
我的 wpf 应用程序中有两个 wpf 窗口。
1)当我点击加载按钮时,它会加载第二个窗口。secong 窗口需要 15 到 20 秒才能加载。
如何添加进度条以显示加载窗口以及第二个窗口加载时关闭进度条。
有很多方法可以做到这一点。一种简单的方法是创建带有进度条或等待动画的第三个窗口或面板。第三个窗口负责加载您的第二个窗口,并在您单击第一个窗口上的加载按钮后立即显示。当第二个窗口的加载完成时,带有进度条的第三个窗口将关闭并显示第二个窗口。
希望这可以帮助。
您可以将 BusyIndicator 用作 WPF 扩展工具包的一部分。你可以在这里下载:http ://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator
在您执行昂贵且耗时的处理之前立即加载新窗口时,您可以将 IsBusy 设置为 true。处理完成后,将 IsBusy 设置回 false。此方法涉及将您的 XAML 包装在第二个窗口的 BusyIndicator 中,这可能是您想要的,也可能不是您想要的。
我最近正在为我的应用程序开发一个加载窗口,您可以在其中单击应用程序,加载大约需要 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();
}));
}
}