微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.net – 如何使椭圆闪烁?

我试图在WPF中进行自定义控件.我希望它模拟可以闪烁的LED的行为.

控件有三种状态:开,关和闪烁.

我知道如何通过后面的代码设置On和Off,但是这个WPF动画的东西只是让我疯了!我无法获得任何动画效果.计划是拥有一个名为state的财产.当用户将值设置为闪烁时,我希望控件在绿色和灰色之间切换.我假设我需要一个依赖属性,但不知道.
之前我有更多的xaml,但只是删除了所有.它似乎没有做任何事情.
我希望以尽可能最好的方式做到这一点,但在这一点上,我会采取任何措施.我正在写一个在这一点上手动改变颜色的线程的一半.

<UserControl x:Class="WpfAnimation.LED"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">

<Grid>
    <Ellipse x:Name="MyLight" Height="Auto" Width="Auto"/>
</Grid>

</UserControl>

解决方法

您可以使用自动反转和重复的动画执行此操作(这适用于Silverlight):

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Blinker.MainPage"
    Width="640" Height="480" Loaded="UserControl_Loaded">
    <UserControl.Resources>
        <Storyboard x:Name="Blink" AutoReverse="True" RepeatBehavior="Forever">
            <ColorAnimationUsingKeyFrames BeginTime="00:00:00"
              Storyboard.TargetName="ellipse"
              Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                 <EasingColorKeyFrame KeyTime="00:00:01" Value="Gray"/>
            </ColorAnimationUsingKeyFrames>
         </Storyboard>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
         <Ellipse x:Name="ellipse" Fill="Green" stroke="Black"/>
    </Grid>
</UserControl>

然后在控件加载或设置属性时启动动画 – 除非你不需要依赖属性

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

        this.blinking = value;
    }
}

或在启动时:

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

这是另一种在WPF中使用visualstatemanager方法 – 它也适用于Silverlight:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="BlinkerApp.Blinker"
x:Name="UserControl"
d:DesignWidth="100" d:DesignHeight="100">
<Grid x:Name="LayoutRoot">
    <visualstatemanager.VisualStateGroups>
        <VisualStateGroup x:Name="BlinkStates">
            <VisualState x:Name="Blinking">
                <Storyboard AutoReverse="True" RepeatBehavior="Forever">
                    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ellipse" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                        <SplineColorKeyFrame KeyTime="00:00:01" Value="Gray"/>
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
            <VisualState x:Name="Stopped"/>
        </VisualStateGroup>
    </visualstatemanager.VisualStateGroups>
    <Ellipse x:Name="ellipse" Fill="Green" stroke="Black"/>
</Grid>

然后让IsBlinking属性切换视觉状态:

namespace BlinkerApp
{
    using System.Windows;
    using System.Windows.Controls;

/// <summary>
/// Interaction logic for Blinker.xaml
/// </summary>
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;    
        }
    }       
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐