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