简体   繁体   English

WPF UI 控件背景更改

[英]WPF UI Control Background changing

The code below happens too fast to see the change.下面的代码发生得太快,无法看到变化。 Is there a way to slow this down without modifying the xaml style?有没有办法在不修改 xaml 样式的情况下减慢速度? My only thought was to run a task but that seems a bit overdone.我唯一的想法是运行一个任务,但这似乎有点过头了。 Thoughts?想法?

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

This seems to work... any caveats?这似乎有效......有什么警告吗?

    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);
    }

The basic pattern:基本模式:

// 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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM