添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I've been learning WPF. I tried using Threading in my WPF application, but I think I missed something.

I've placed a button and a label in the window. If I press the button, I want the label to change it's background color... after 2 seconds, set the original background color.

Here's the XAML and the code that I've been trying.

MainWindow.XAML

<Window x:Class="UITest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:UITest"
        mc:Ignorable="d"
        Title="MainWindow" Height="519" Width="656">
    <StackPanel>
        <Button x:Name="button1" Width="60" Height="30" Content="Button" Margin="10 10 0 0" BorderThickness="0" BorderBrush="{x:Null}" Padding="0" HorizontalAlignment="Left" VerticalAlignment="Top">
        </Button>
        <Label x:Name="label1" Width="100" Height="30" Margin="10 10 0 0" BorderThickness="0" BorderBrush="{x:Null}" Padding="0" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Bisque">
        </Label>
    </StackPanel>
</Window>

MainWindow.XAML.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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;
namespace UITest
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()
            InitializeComponent();
            this.button1.Click += Button1_Click;
        private void Button1_Click(object sender, RoutedEventArgs e)
            label1.Background = Brushes.Black;
            Thread.Sleep(2000);
            label1.Background = Brushes.Bisque;

You are blocking UI thread. Try this instead:

private async void Button1_Click(object sender, RoutedEventArgs e)
        label1.Background = Brushes.Black;
        await Task.Delay(2000);
        label1.Background = Brushes.Bisque;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.