我有一个wpf窗口,它有四个标签m1,m2,m3,m4。当我单击窗口中的按钮时,所有四个标签应该一个接一个地显示。但是我立即显示所有标签,这不应该发生。
private void button_Click(object sender, RoutedEventArgs e)
{
m1.Content = DateTime.Now.ToString("HH:mm:ss") + " Establishing Connection";
//some code
m2.Content = DateTime.Now.ToString("HH:mm:ss") + " Connected";
//some code
m3.Content = DateTime.Now.ToString("HH:mm:ss") + " Processing";
//some code
m4.Content = DateTime.Now.ToString("HH:mm:ss") + " Finished";
}
运行应用程序后,窗口显示以下所有标签,而不是一次显示,而不是根据时间一个接一个地显示
10:14:20 建立连接
10:14:24 已连接
10:14:26 处理
10:14:30 完成
您正在寻找的容器称为 StackPanel,它会自动将任何元素一个接一个地放入其中。
例
<StackPanel Orientation="Vertical">
<Label Name="m1"/>
<Label Name="m2"/>
<Label Name="m3"/>
<Label Name="m4"/>
</StackPanel>
做的是在设置Label
的Content
后,您可以切换Label
的Visibility
,这样您就不会一次获得所有Label
,而是一一获得它。正如@Dan建议的那样,使用StackPanel
来安排Label
。还要注意一个要点,即每次操作后都会更新 UI 线程。
下面是我使用的示例代码。
MainWindow.xaml
<StackPanel>
<Button Content="Testing" Click="Button_Click" />
<Label Name="m1" Visibility="Hidden" />
<Label Name="m2" Visibility="Hidden" />
<Label Name="m3" Visibility="Hidden" />
<Label Name="m4" Visibility="Hidden" />
</StackPanel>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent ();
}
private async void Button_Click (object sender, RoutedEventArgs e)
{
m1.Content = DateTime.Now.ToString ("HH:mm:ss") + " Establishing Connection";
m1.Visibility = Visibility.Visible;
await Task.Run (() => Thread.Sleep (2000));
m2.Content = DateTime.Now.ToString ("HH:mm:ss") + " Connected";
m2.Visibility = Visibility.Visible;
await Task.Run (() => Thread.Sleep (2000));
m3.Content = DateTime.Now.ToString ("HH:mm:ss") + " Processing";
m3.Visibility = Visibility.Visible;
await Task.Run (() => Thread.Sleep (2000));
m4.Content = DateTime.Now.ToString ("HH:mm:ss") + " Finished";
m4.Visibility = Visibility.Visible;
await Task.Run (() => Thread.Sleep (2000));
}
}
我希望它有所帮助。还可以使用BackgroundWorker
来避免 UI 冻结。
下面是使用BackgroundWorker
的代码
MainWindow.xaml.cs
public partial class MainWindow : Window
{
private readonly BackgroundWorker worker = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync();
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
m1.Visibility = Visibility.Hidden;
m2.Visibility = Visibility.Hidden;
m3.Visibility = Visibility.Hidden;
m4.Visibility = Visibility.Hidden;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
Dispatcher.Invoke(() =>
{
m1.Content = DateTime.Now.ToString("HH:mm:ss") + " Establishing Connection";
m1.Visibility = Visibility.Visible;
});
Thread.Sleep(2000);//your code
Dispatcher.Invoke(() =>
{
m2.Content = DateTime.Now.ToString("HH:mm:ss") + " Connected";
m2.Visibility = Visibility.Visible;
});
Thread.Sleep(2000);//your code
Dispatcher.Invoke(() =>
{
m3.Content = DateTime.Now.ToString("HH:mm:ss") + " Processing";
m3.Visibility = Visibility.Visible;
});
Thread.Sleep(2000);//your code
Dispatcher.Invoke(() =>
{
m4.Content = DateTime.Now.ToString("HH:mm:ss") + " Finished";
m4.Visibility = Visibility.Visible;
});
Thread.Sleep(2000);//your code
}
}