Wdrażanie pauzy w WPF
Tutaj masz prosty program WPF:
<code><!-- Updater.xaml --> <Window x:Class="Update.Updater" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}"> <Grid> <StackPanel> <Button Click="Button_Click" Height="50"></Button> <Label Content="{Binding Label1Text}" Height="50"></Label> <Label Content="{Binding Label2Text}" Height="50"></Label> </StackPanel> </Grid> </Window> // Updater.xaml.cs using System.Threading; using System.Windows; namespace Update { public partial class Updater : Window { public Updater() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Label1Text = "It is coming..."; Thread.Sleep(3000); Label2Text = "It is here!"; } public string Label1Text { get { return (string)GetValue(CategoryProperty); } set { SetValue(CategoryProperty, value); } } static readonly DependencyProperty CategoryProperty = DependencyProperty.Register("Label1Text", typeof(string), typeof(Updater)); public string Label2Text { get { return (string)GetValue(Label2TextProperty); } set { SetValue(Label2TextProperty, value); } } static readonly DependencyProperty Label2TextProperty = DependencyProperty.Register("Label2Text", typeof(string), typeof(Updater)); } } </code>
Intencją jest, aby po kliknięciu przycisku pojawiła się pierwsza etykietaIt is coming...
. Następnie program śpi przez 3 sekundy, a na końcu wyświetla się druga etykietaIt is here!
. Jednak poniższa implementacja poniżej nie działa. Jeśli go uruchomisz i klikniesz przycisk, dzieje się tak: Program śpi przez 3 sekundy, a następnie wyświetlane są jednocześnie dwa teksty etykiet. Czy wiesz, jak poprawić program, aby działał zgodnie z przeznaczeniem?