等待 WPF 窗口加载的屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14814882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Waiting Screen for WPF window Loading
提问by uncia
I am navigating from some window window1 to Mainwindow
我正在从某个窗口 window1 导航到 Mainwindow
In my MainWindow_loaded Method i have too much computation so when i navigate to main window windows goes white untill all computation finished and window loaded
在我的 MainWindow_loaded 方法中,我有太多的计算,所以当我导航到主窗口时,窗口变为白色,直到所有计算完成并加载窗口
I tried in MainWindow
我在 MainWindow 中尝试过
private void MainWindow_Loaded_1(object sender, RoutedEventArgs e)
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (o, ea) =>
{
do large computation
};
worker.RunWorkerCompleted += (o, ea) =>
{
_busy.IsBusy = false;
};
_busy.IsBusy = true;
worker.RunWorkerAsync();
}
But problem is that it navigates to Mainwindow without computation of necessary data in loaded event handler and doesnt even show waiting bar in UI ? ? Is it possible to show waiting bar and all computation of data in MainWindow_Loaded?
但问题是它导航到 Mainwindow 而没有在加载的事件处理程序中计算必要的数据,甚至没有在 UI 中显示等待栏?? 是否可以在 MainWindow_Loaded 中显示等待条和所有数据计算?
I have used ExtendedWpfToolkit for _busy which is busy indicator
我已经将 ExtendedWpfToolkit 用于 _busy,这是繁忙的指示器
采纳答案by tnw
You can subscribe your background worker to report progress.
您可以订阅后台工作人员以报告进度。
worker.WorkerReportsProgress = true;
And now you can have this progress report be triggered by an event you subscribe to.
现在,您可以通过订阅的事件触发此进度报告。
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
In doing so, you can create a progress bar than can update itself based on this worker_ProgressChangedevent, triggered by your computation.
这样做时,您可以创建一个进度条worker_ProgressChanged,然后根据您的计算触发的此事件更新自身。
It appears you've already figured out IsBusy, so you can have your progress bar show only when this is true.
看来您已经想通了IsBusy,因此您可以仅在此情况下显示进度条。
回答by Anand Murali
You can use BusyIndicatorcontrol. It is part of the Extended WPF ToolKit.
您可以使用BusyIndicator控件。它是扩展 WPF 工具包的一部分。
I have created a sample app using it. Below is screen shot of the app which displays the loop count.
我已经使用它创建了一个示例应用程序。下面是显示循环计数的应用程序的屏幕截图。


Hereis a tutorial on how to use it.
这是有关如何使用它的教程。
Sample code.
示例代码。
Note: You need to download the tool kit and add a reference to Xceed.Wpf.Toolkit.dllin your project.
注意:您需要下载工具包并Xceed.Wpf.Toolkit.dll在您的项目中添加引用。
XAML code:
XAML 代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WPFTool="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
Title="MainWindow" Height="200" Width="300" Loaded="Window_Loaded">
<WPFTool:BusyIndicator Name="BusyIndicator">
<Grid>
</Grid>
</WPFTool:BusyIndicator>
</Window>
CodeBehind:
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BusyIndicator.IsBusy = true;
BusyIndicator.BusyContent = "Initializing...";
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (o, a) =>
{
for (int index = 0; index < 5; index++)
{
Dispatcher.Invoke(new Action(() =>
{
BusyIndicator.BusyContent = "Loop : " + index;
}), null);
Thread.Sleep(new TimeSpan(0, 0, 1));
}
};
worker.RunWorkerCompleted += (o, a) =>
{
BusyIndicator.IsBusy = false;
};
worker.RunWorkerAsync();
}
}
}
回答by Gaurav Panwar
Easiest way of doing this is use of WPF Progress Bar with xaml, below is the code:
最简单的方法是使用带有 xaml 的 WPF 进度条,以下是代码:
<ProgressBar IsIndeterminate="True" Visibility="True"/>
<ProgressBar IsIndeterminate="True" Visibility="True"/>
where IsIndeterminate 'true' will behave like a waiting progress and 'false' will show exact progress. Also set visibility when you want to show and when hide if you are using as a waiting progress.
其中 IsIndeterminate 'true' 将表现得像一个等待进度,而 'false' 将显示确切的进度。还可以在要显示时设置可见性,如果用作等待进度,则在隐藏时设置可见性。
回答by Peter Karlsson
One option would be to create an overlay whose visibility depends on IsBusy, sample coming once I'm not on my phone
一种选择是创建一个覆盖层,其可见性取决于 IsBusy,一旦我不在手机上,示例就会出现

