我希望我的TextBox
间歇性地闪烁。
我有这个方法
private void abilitaAltroToken(int indiceRiga, Grid grid)
{
UIElement element = grid.Children[indiceRiga];
Label label = (Label)element;
element = grid.Children[++indiceRiga];
TextBox textBox = (TextBox)element;
textBox.Background = Brushes.Blue;
label.Background = Brushes.Blue;
Thread.Sleep(1000);
label.Background = Brushes.White;
Thread.Sleep(1000);
label.Background = Brushes.Blue;
Thread.Sleep(1000);
label.Background = Brushes.White;
Thread.Sleep(1000);
label.Background = Brushes.Blue;
}
此代码不会返回错误,但不会发生闪烁。
答案 0 :(得分:5)
首先,你不应该在主(UI)线程代码中放置Thread.Sleep,这会使UI线程进入休眠状态,并且你不会在UI上看到任何变化。
就个人而言,我会使用动画(在XAML中也是如此)和Triggers / VisualStates来实现你在这里尝试的东西。
但是,由于我对XAML的了解不多,以下是实现标签闪烁的程序代码:
var colorAnim = new ColorAnimationUsingKeyFrames()
{
KeyFrames = new ColorKeyFrameCollection
{
new DiscreteColorKeyFrame(Colors.White, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.5))),
new DiscreteColorKeyFrame(Colors.Blue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1))),
new DiscreteColorKeyFrame(Colors.White, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1.5))),
new DiscreteColorKeyFrame(Colors.Blue, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2))),
}
};
var storyBoard = new Storyboard();
storyBoard.Children.Add(colorAnim);
Storyboard.SetTarget(storyBoard, label);
Storyboard.SetTargetProperty(storyBoard, new PropertyPath("(Background).(SolidColorBrush.Color)"));
storyBoard.Begin();
基本上,我从您的问题中翻译了以下代码段:
Thread.Sleep(1000);
label.Background = Brushes.White;
Thread.Sleep(1000);
label.Background = Brushes.Blue;
Thread.Sleep(1000);
label.Background = Brushes.White;
Thread.Sleep(1000);
label.Background = Brushes.Blue;
答案 1 :(得分:2)
使用Timer对象
以下是示例代码
private static System.Timers.Timer aTimer;
private static bool blinkFlag;
private Label label;
private void abilitaAltroToken(int indiceRiga,Grid grid)
{
UIElement element = grid.Children[indiceRiga];
label = (Label)element;
element = grid.Children[++indiceRiga];
TextBox textBox = (TextBox)element;
textBox.Background = Brushes.Blue;
label.Background = Brushes.Blue;
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 1 seconds (1000 milliseconds).
aTimer.Interval = 1000;
aTimer.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
label.Background = (blinkFlag?Brushes.Blue:Brushes.White);
blinkFlag=!blinkFlag;
aTimer.Interval = 1000;
aTimer.Enabled = true;
}
答案 2 :(得分:1)
你不应该睡在“UI线程”中。我建议您尝试使用DispatcherTimer
代替。有关详细信息,请参阅以下链接:http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx
答案 3 :(得分:1)
双击计时器工具箱或将计时器拖到表单中。添加以下代码:
private void timer1_Tick(object sender, EventArgs e)
{
if (tickcolor) txtAno.BackColor = Color.Red;
if (!tickcolor) txtAno.BackColor = Color.White;
tickcolor = !tickcolor;
}
private void txtAno_KeyPress(object sender, KeyPressEventArgs e)
{
timer1.Stop();
txtAno.BackColor = Color.White;
}
在form_load事件中添加以下内容:
timer1.Interval = 500;
timer1.Start();
txtAno.Focus();
声明变量:private bool tickcolor = true;
这将使文本框txtAno闪烁红色和白色。