How do I make an ellipse blink?

前端 未结 2 529
面向向阳花
面向向阳花 2021-01-01 03:31

I am trying to make a custom control in WPF. I want it to simulate the behavior of a LED that can blink.

There are three states to the control: On, Off, and Blinkin

2条回答
  •  梦毁少年i
    2021-01-01 04:04

    You can do this with an animation that auto-reverses and repeats (this is for Silverlight):

    
        
            
                
                     
                
             
        
        
             
        
    
    

    and then start the animation when the control loads or when a property is set - you don't need a dependency property unless you

    private bool blinking;
    public bool IsBlinking
    {
        get
        {
           return blinking;
        }
        set
        {
            if (value)
            {
                 this.Blink.Begin();
            }
            else
            {
                 this.Blink.Stop();
            }
    
            this.blinking = value;
        }
    }
    

    or on startup:

    private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        this.Blink.Begin();
    }
    

    Here is another way to do it in WPF - using the VisualStateManager - that will also work in Silverlight:

    
    
        
            
                
                    
                        
                            
                        
                    
                
                
            
        
        
    
    

    and then have the IsBlinking property switch the visual state:

    namespace BlinkerApp
    {
        using System.Windows;
        using System.Windows.Controls;
    
    /// 
    /// Interaction logic for Blinker.xaml
    /// 
    public partial class Blinker : UserControl
    {
        private bool blinking;
    
        public Blinker()
        {
            this.InitializeComponent();
        }
    
        public bool IsBlinking
        {    
            get    
            {       
                return blinking;    
            }    
    
            set    
            {        
                if (value)        
                {
                    VisualStateManager.GoToState(this, "Blinking", true);
                }        
                else        
                {
                    VisualStateManager.GoToState(this, "Stopped", true);
                }        
    
                this.blinking = value;    
            }
        }       
    }
    }
    

提交回复
热议问题