你可以使用BusyIndicator
控制。它是扩展 WPF 工具包的一部分。
我已经使用它创建了一个示例应用程序。下面是显示循环计数的应用程序的屏幕截图。
这是有关如何使用它的教程。
示例代码。
注意:您需要下载工具包并Xceed.Wpf.Toolkit.dll
在您的项目中添加对的引用。
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>
代码隐藏:
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();
}
}
}