大佬教程收集整理的这篇文章主要介绍了.net – 如何使椭圆闪烁?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
控件有三种状态:开,关和闪烁.
我知道如何通过后面的代码设置On和Off,但是这个WPF动画的东西只是让我疯了!我无法获得任何动画效果.计划是拥有一个名为state的财产.当用户将值设置为闪烁时,我希望控件在绿色和灰色之间切换.我假设我需要一个依赖属性,但不知道.
之前我有更多的xaml,但只是删除了所有.它似乎没有做任何事情.
我希望以尽可能最好的方式做到这一点,但在这一点上,我会采取任何措施.我正在写一个在这一点上手动改变颜色的线程的一半.
<UserControl x:Class="WpfAnimation.LED" xmlns="http://scheR_844_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_844_11845@as.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Grid> <Ellipse x:Name="MyLight" Height="Auto" Width="Auto"/> </Grid> </UserControl>
<UserControl xmlns="http://scheR_844_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_844_11845@as.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://scheR_844_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_844_11845@as.microsoft.com/winfx/2006/xaml" xmlns:d="http://scheR_844_11845@as.microsoft.com/expression/blend/2008" xmlns:mc="http://scheR_844_11845@as.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 – 如何使椭圆闪烁?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。