.net – 如何使椭圆闪烁?

.net – 如何使椭圆闪烁?,第1张

概述我试图在WPF中进行自定义控件.我希望它模拟可以闪烁的LED的行为. 控件有三种状态:开,关和闪烁. 我知道如何通过后面的代码设置On和Off,但是这个WPF动画的东西只是让我疯了!我无法获得任何动画效果.计划是拥有一个名为state的财产.当用户将值设置为闪烁时,我希望控件在绿色和灰色之间切换.我假设我需要一个依赖属性,但不知道. 之前我有更多的xaml,但只是删除了所有.它似乎没有做任何事情. 我试图在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:

<UserControlxmlns="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;            }    }       }}
总结

以上是内存溢出为你收集整理的.net – 如何使椭圆闪烁?全部内容,希望文章能够帮你解决.net – 如何使椭圆闪烁?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1078867.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存