c# - 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? 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
}
推荐阅读
- html-framework-7 - 导航到其他页面后使用 DOM7 执行某些操作
- javascript - JQuery目标向上或向下滚动,但不是像素
- php - 具有自定义操作的 Drupal 8 表单
- html - 如何使用绝对位置平滑拉伸 div?
- react-native - 有天赋的聊天问题,消息在文本输入时跳到屏幕上
- java - 带有描述的 Hotspot VM 操作列表
- php - 如何在sql中的3表查询中显示确切结果
- javascript - 努力定位使用 jQuery 创建的 div 元素
- html - 使用 Angular 或 Angular 材质的 HTML 表格单元格中的垂直线和水平双向箭头
- node.js - 如何在Node js中连续接收tcp连接数据