WPF UI 控件背景更改

WPF UI Control Background changing

下面的代码发生得太快,看不到变化。有没有办法在不修改 xaml 样式的情况下减慢速度?我唯一的想法是 运行 一项任务,但这似乎有点过头了。想法?

       switch (e.Key)
        {
            case Key.Escape:
                ButtonStop.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#353535"));
                StopButton();
                ButtonStop.ClearValue(BackgroundProperty);
                break;
        }

这似乎有效...有什么注意事项吗?

    private static async void PressBorder(Border control)
    {
        StopButton();
        var wait = Task.Delay(250);
        control.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#353535"));
        await wait;
        control.ClearValue(BackgroundProperty);
    }

基本模式:

// avoid 'async void' almost everywhere else. Ok for an event handler
private async void HandleOnKeyDown(object sender, KeyEventArgs e)
{
  try  // async void needs to do its own error handling 
  {    
        switch (e.Key)
        {
            case Key.Escape:
                ButtonStop.Background = ...;

                // sandwich StopButton() in case it takes some time too   
                var waitTask = Task.Delay(250);  
                StopButton();
                await waitTask; 
                ButtonStop.ClearValue(BackgroundProperty);
                break;
        }

  }
  catch(Exception e)
  {
     // report it
  }

  // general cleanup & restore

}