加载新窗口时更新WPF窗口标签

时间:2014-07-25 20:01:06

标签: c# wpf backgroundworker splash-screen

我已尝试过包括后台工作器和调度程序在内的所有内容,但我无法弄清楚如何执行此操作。

这是我想要实现的目标。

我的WPF' MainWindow'它有一系列在渲染之前完成的功能。完成总共需要20秒。因此,我创建了一个名为' LoadWindow'的窗口。我有' LoadWindow' ContentRendered事件设置为加载我的' MainWindow'然后在完成后关闭。我基本上创建了一个SplashScreen。

 private void Window_ContentRendered(object sender, EventArgs e)
 {
     MainWindow mainWindow = new MainWindow();
     mainWindow.Show();
     this.Close();
 }

我想要一个标签,让我们称它为“splashLabel”。在我的主要内容中触发每个功能时进行更新。代码背后。在我的网格layoutRoot Initialize事件期间调用这些函数。正如我目前所拥有的那样,即使内容已被更改,我的splashLabel也无法渲染。

 private void layOutRoot_Initialized(object sender, EventArgs e)
 {
     LoadWindow.splashLabel.content = "Function 1";
     Function1();
     LoadWindow.splashLabel.content = "Function 2";
     Function2();
     LoadWindow.splashLabel.content = "Function 3";
     Function3();
 }

请指出我正确的方向!我的谷歌搜索时代没有帮助。

1 个答案:

答案 0 :(得分:1)

退一步,你有一个慢速加载窗口要报告进度吗?

如果是这样,过去我发现效果很好的是为您的控件添加一个隐藏所有内容的叠加层,并在其上添加标签以报告状态。将其可见性绑定到IsBusy属性。将您的标签绑定到Status属性。

然后在您的视图模型中,假设MVVM,将您的函数定义为异步。然后编写一个简单的函数,依次调用这些函数并更新所提到的属性。有点像...

Private async void DoLoadingTasks()
{
     IsBusy = true;
     await Function1();
     Status = "step complete";
     ....
     IsBusy = false;
}

从视图模型构造函数中调用该方法,您应该具有响应式UI和进度通知。

这样做你想要的吗?

相关问题