如何使用加载百分比

时间:2016-11-27 15:12:15

标签: c# wpf splash-screen dispatchertimer

启动应用程序时,我必须执行大量操作。 我必须显示加载百分比的闪屏。 这个想法是第一次记录加载所需的时间,然后用其他数据序列化。以下时间我有了在启动画面上显示百分比的所有必要条件。

一开始,我创建了一个带有一些信息的闪屏窗口

var bmpSplash = new BitmapImage(new Uri(@"pack://application:,,,/Resources/Images/splashScreen.png", UriKind.RelativeOrAbsolute));
splashScreen = new EmptyWindow.EmptyWindow(bmpSplash) { SizeToContent = SizeToContent.Manual, Width = 612, Height = 378, Background = Brushes.Transparent, WindowStyle = WindowStyle.None, AllowsTransparency = true, Topmost = true };
...
var tbPercetage = new TextBlock() { Text="AAAA", FontSize = 16, Width = splashScreen.Width, TextAlignment = TextAlignment.Left, Height = 50, VerticalAlignment = VerticalAlignment.Bottom, Margin = new Thickness(150, 0, 0, 50) };
...
splashScreen.grdMain.Children.Add(otbVersion4);
splashScreen.Show();

然后我创建一个Dispatcher计时器,它必须更新百分比:

DispatcherTimer tmrSplashPercentage = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(125) };
tmrSplashPercentage.Tick += (sender2, args) =>
{
((TextBlock)splashScreen.grdMain.Children[3]).Dispatcher.Invoke(() => ((TextBlock)splashScreen.grdMain.Children[3]).Text= DateTime.Now.Ticks.ToString(), DispatcherPriority.Background);
};<---- it is the 4th children

Dispatcher.CurrentDispatcher.Invoke(new Action(() => { tmrSplashPercentage.Start(); }));

我使用Invoke因为我已经看到通过这样做我可以强制更新。 但是如果我在计时器滴答中设置一个断点,我已经看到它在所有加载过程完成之前都没有被触发。 因此要么它没有启动,要么没有触发tick事件。

感谢您的帮助! 帕特里克

--- --- ADD

很抱歉忘了说事件发生在MainView()中,然后继续在WindowViewBase_Loaded事件中。

我知道我现在可能会使用一个计时器,但是在每一步之后都会进行多次更新,但这不会让它变得精确......

0 个答案:

没有答案
相关问题