C#如何使TextBox闪烁

 Ymgif影像--阿雅XX_506 发布于 2023-02-13 19:02

我想让我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;       
}

此代码不会返回错误,但不会发生闪烁.

1 个回答
  • 首先,您不应该将Thread.Sleep放在您的Main(UI)线程代码中,这将使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;
    

    2023-02-13 19:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有