下面的代码发生得太快,看不到更改。有没有办法在不修改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);
}
答案 0 :(得分:1)
基本模式:
// 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
}