使图标闪烁wpf应用程序

时间:2013-11-19 06:53:30

标签: c# wpf icons

我有一个图标。

 imgstop.Visibility = System.Windows.Visibility.Visible;

如何让图标显示为闪烁?

我尝试了以下代码:

  public void blinkIconStopHide()
        {
            blinkTimerStop.Enabled = true;
            blinkTimerStop.Interval = 15000;
             imgstop.Visibility = System.Windows.Visibility.Hidden;
             blinkTimerStop.Elapsed += new System.Timers.ElapsedEventHandler(tick_to_hideStop);  
        }
        private void tick_to_hideStop(object sender, EventArgs e)
        {
            blinkIconStopShow();
        }  
        public void blinkIconStopShow()
        {
            blinkTimerStop.Enabled = true;
            blinkTimerStop.Interval = 15000;
            imgstop.Visibility = System.Windows.Visibility.Visible;
            blinkTimerStop.Elapsed += new System.Timers.ElapsedEventHandler(tick_to_showStop);   
        }
        private void tick_to_showStop(object sender, EventArgs e)
        {
            blinkIconStopHide();
        }  

但屏幕上似乎没有任何东西闪烁。

2 个答案:

答案 0 :(得分:6)

动画图标/图像怎么样?

XAML

<BeginStoryboard>
   <Storyboard>
       <DoubleAnimation From="1" To="0" Storyboard.TargetProperty="Opacity" 
                        Duration="0:0:0.4" RepeatBehavior="Forever"
                        AutoReverse="True"/>
   </Storyboard>
</BeginStoryboard>

C#

DoubleAnimation da = new DoubleAnimation();
da.From = 1;
da.To = 0;
da.Duration = new Duration(TimeSpan.FromSeconds(2));
da.AutoReverse = true;
da.RepeatBehavior = RepeatBehavior.Forever;
yourimage.BeginAnimation(OpacityProperty, da);

答案 1 :(得分:1)

以下代码将通过切换可见性让您的Icon每秒闪烁一次。通过更改DispatcherTimer的间隔,您可以更快或更慢地闪烁图标。

DispatcherTime _blinkTimer = new DispatcherTimer();

public void StartBlinking() {
  _blinkTimer.Interval = TimeSpan.FromSeconds(1);
  _blinkTimer.Elapsed += ToggleIconVisibility;
  _blinkTimer.Start();
}

public void StopBlinking() {
  _blinkTimer.Stop();
  _blinkTimer.Elapsed -= ToggleIconVisibility;
}

private void ToggleIconVisibility(object sender, EventArgs e)
{
  imgstop.Visibility = !imgstop.Visibility;
}