状态栏文字闪烁



当用户试图在我的表单中执行非法操作时,我试图实现一个警告系统。这个想法是调用StatusBarFade方法,给它应该写的参数,然后让该方法显示文本,通过改变它的颜色让它闪烁一秒半,然后再停留一秒半,之后文本将消失。

每隔一段时间闪烁一次,而文本正常消失。

请注意,我知道这段代码相当混乱,肯定有更好的方法来做到这一点,但是因为我不完全知道委托是如何工作的,所以我不知道如何正确地利用它们。希望有人能解释我做错了什么。无论如何,经过一些测试后,我意识到最好设置两个计时器。问题是这段代码每隔一段时间就可以工作。

    private Timer timer = new System.Timers.Timer();
    private Timer timerColor = new System.Timers.Timer();
    private void StatusBarFade(string ispis)
    {
        statusBar1AS2.Content = ispis;
        int i = 0;
        timerColor.Interval = 100;
        timerColor.AutoReset = true;
        timerColor.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    ++i;
                    if (statusBar1AS2.Foreground == Brushes.Black)
                        statusBar1AS2.Foreground = Brushes.Gold;
                    else
                        statusBar1AS2.Foreground = Brushes.Black;
                    if (i > 15)
                    {
                        statusBar1AS2.Foreground = Brushes.Black;
                        i = 0;
                        timerColor.Stop();
                    }
                }));
        };
        timerColor.Start();
        timer.Interval = 3000;
        timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
        {
            timer.Stop();
            this.Dispatcher.BeginInvoke(new Action(() => { statusBar1AS2.Content = ""; }));
        };
        timer.Start();
    }

就我对委托的理解而言,我不应该向计时器添加相同的委托。每次更改文本时发生的事件,但仅一次,例如在构造函数中。问题是我不知道如何在代码中使用计数器i

你可以简单地使用下面的动画:

var animation = new ColorAnimation
{
    From = Colors.Black,
    To = Colors.Gold,
    AutoReverse = true,
    Duration = TimeSpan.FromSeconds(0.1),
    RepeatBehavior = new RepeatBehavior(TimeSpan.FromSeconds(1.5))
};
animation.Completed += (o, e) => statusBar.Content = string.Empty;
statusBar.Foreground = new SolidColorBrush();
statusBar.Foreground.BeginAnimation(SolidColorBrush.ColorProperty, animation);

UPDATE:为了延迟消息的删除,您当然可以使用计时器。您也许可以像下面这样编写StatusBarFade方法,并使用DispatcherTimer:

private void StatusBarFade(string message)
{
    statusBar.Content = message;
    var animation = new ColorAnimation
    {
        From = Colors.Gold,
        To = Colors.Black,
        AutoReverse = true,
        Duration = TimeSpan.FromSeconds(0.1),
        RepeatBehavior = new RepeatBehavior(TimeSpan.FromSeconds(1.5)),
    };
    statusBar.Foreground = new SolidColorBrush();
    statusBar.Foreground.BeginAnimation(SolidColorBrush.ColorProperty, animation);
    var timer = new DispatcherTimer
    {
        Interval = TimeSpan.FromSeconds(4.5)
    };
    timer.Tick +=
        (o, e) =>
        {
            timer.Stop();
            statusBar.Content = string.Empty;
        };
    timer.Start();
}

try with storyboard。

<Storyboard x:Key="TestBlinkStoryboard" AutoReverse="True" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="textBlock">
            <EasingColorKeyFrame KeyTime="0" Value="Black"/>
            <EasingColorKeyFrame KeyTime="0:0:0.5" Value="#FFE91414">
                <EasingColorKeyFrame.EasingFunction>
                    <BounceEase EasingMode="EaseInOut"/>
                </EasingColorKeyFrame.EasingFunction>
            </EasingColorKeyFrame>
        </ColorAnimationUsingKeyFrames>
    </Storyboard>

@Clemens的答案就是你想要的。我只是想找出这种行为的原因。

您所遇到的问题是由于在计时器迭代完成后附加委托而不删除它们。

这就是为什么你在每次偶数发生时都看到这种行为,因为在这些时候,偶数个委托附加到计时器的流逝事件上,因此它几乎以2,4,6,…因此,最终从黑色->黑色,我们从来没有看到切换到金色。动画的总时间也以同样的方式逐渐下降。

解决这个问题的方法是(请不要使用这个。把它当作一个案子。使用@Clemens方法或者直接在xaml中创建Storyboard
private int i = 0;
private void StatusBarFade(string ispis) {
  i = 0;
  statusBar1AS2.Content = ispis;
  timerColor.Interval = 100;
  timerColor.AutoReset = true;
  timerColor.Elapsed += TimerColorOnElapsed;
  timerColor.Start();
}
private void TimerColorOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) {
  Dispatcher.BeginInvoke(
    (new Action(
      () => {
        ++i;
        statusBar1AS2.Foreground = i % 2 == 0 || i > 15 ? Brushes.Black : Brushes.Gold;
        if (i < 30)
          return;
        timerColor.Stop();
        timerColor.Elapsed -= TimerColorOnElapsed;
        statusBar1AS2.Content = string.Empty;
      })));
}

最新更新