How do I make an ellipse blink?

前端 未结 2 544
面向向阳花
面向向阳花 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条回答
  •  滥情空心
    2021-01-01 04:12

    To allow for greater control of the blink rate and such in your code behind, I'd suggest having a routed event in your UserControl called Blink:

    public static readonly RoutedEvent BlinkEvent = EventManager.RegisterRoutedEvent("Blink", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(LedControl));
    public event RoutedEventHandler Blink
    {
        add { AddHandler(BlinkEvent, value); }
        remove { RemoveHandler(BlinkEvent, value); }
    }
    

    In your code behind you can set up a timer to raise the event however often you like (this also gives you the opportunity to blink the light a single time whenever you want:

    RaiseEvent(new RoutedEventArgs(LedControl.Blink));
    

    Now in XAML, the following code would make a glow visible, and set the fill property of your ellipse (ledEllipse) to a bright green radial gradient, then return the fill value to a dim 'unlit' green (which you could change to gray if you like). You can simply change the duration to make the blink last longer.

    
        
            
                
                    
                        
                        
                            
                                
                                    
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                                
                                    
                                        
                                            
                                            
                                            
                                            
                                        
                                    
                                
                            
                        
                    
                
            
        
    
    

    Also, I am directly referencing the ellipse 'ledEllipse' and it's corresponding DropShadowEffect 'glow' which are defined in the ledControl as following (redLight is just another radial gradient brush that I start my led's fill property at):

    
        
            
        
    
    

    Note: The DropShadowEffect was introduced in .Net 3.5, but you could remove that if you don't want a glow effect (but it looks nice on a solid color contrasting background).

提交回复
热议问题