【问题标题】:How do I add a random dispatcher timer for a lucky draw wheel(C#, XAML)如何为幸运抽奖轮添加随机调度程序计时器(C#,XAML)
【发布时间】:2018-06-04 01:51:41
【问题描述】:

我创建了一个幸运抽奖轮,它能够在加载表单时旋转,并在单击按钮时加速和减速旋转。但是,我需要改用调度程序计时器(因为它是幸运抽奖,我不应该设置固定持续时间。)单击时,它应该在持续时间之间旋转,当它停止时会提示应该提取的奖品从一个sqltable。 我目前拥有的代码用于加载事件和情节提要。

private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            myMediaElement.Play();
            var da = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(6)));
            var rt = new RotateTransform();
            ellipse.RenderTransform = rt;
            ellipse.RenderTransformOrigin = new Point(0.5, 0.5);
            da.RepeatBehavior = RepeatBehavior.Forever;
            rt.BeginAnimation(RotateTransform.AngleProperty, da);
        }

<Button x:Name="Spin" Width="189" HorizontalAlignment="Left" Margin="593,717,0,38">
                Spin         
                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <BeginStoryboard>
                            <Storyboard x:Name="Mystoryboard" Completed="Mystoryboardcompleted" FillBehavior="Stop">
                                <DoubleAnimation Storyboard.TargetName="ellipse"
                                                 Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" AutoReverse="False"
                                                 AccelerationRatio="1" Duration="0:0:4" By="2000" />
                                <DoubleAnimation Storyboard.TargetName="ellipse"
                                                 Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" AutoReverse="False"
                                                 DecelerationRatio="1" Duration="0:0:4" By="1200" />
                                <ColorAnimationUsingKeyFrames  Storyboard.TargetName="ellipse1"
                                                 Duration="0:0:4" 
                                                 Storyboard.TargetProperty="(Ellipse.Fill).(SolidColorBrush.Color)">
                                    <ColorAnimationUsingKeyFrames.KeyFrames>
                                        <DiscreteColorKeyFrame KeyTime="0:0:0" Value="White"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:1" Value="Green"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:2" Value="White"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:3" Value="Green"/>
                                        <DiscreteColorKeyFrame KeyTime="0:0:4" Value="Green"/>                                    
                                    </ColorAnimationUsingKeyFrames.KeyFrames>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Button.Triggers>

我已经为调度程序计时器实现了此代码,但是我想在调度程序计时器的持续时间内执行情节提要操作。

private DispatcherTimer _timer = new DispatcherTimer();
        private Random rand = new Random();
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            _timer.Interval = TimeSpan.FromSeconds(rand.Next(5, 10)); // From 5 s to 10 s
            RenderTransformOrigin = new Point(0.5, 0.5);
            DoubleAnimation da = new DoubleAnimation();
            da.From = 0;
            da.To = 360;
            da.Duration = new Duration(TimeSpan.FromSeconds(0.5));
            RotateTransform rt = new RotateTransform();
            ellipse.RenderTransform = rt;
            rt.BeginAnimation(RotateTransform.AngleProperty, da);
        }

【问题讨论】:

  • 您是否尝试过使用 DispatcherTimer?你遇到了什么问题?
  • @ShivaniKatukota 我已经编辑了我的问题,但是我可以在情节提要期间使用调度程序计时器吗?
  • 你根本不需要计时器。只需在单击 Button 时创建并启动新动画,并将其 By 值设置为随机数。
  • @Clemens 因为我的按钮有路由事件(如上图所示)。你的意思是我可以设置角度的范围吗?
  • 欢迎来到 Stack Overflow!欢迎来到 Stackoverflow.com!请让您的问题成为最小、完整和可验证的示例M.C.V E。还要检查how to ask question 以使您的帖子可以回答。

标签: c# mysql sql wpf xaml


【解决方案1】:

假设 XAML 中有一个 Ellipse 元素

<Ellipse Width="200" Height="200" RenderTransformOrigin="0.5,0.5" ...>
    <Ellipse.RenderTransform>
        <RotateTransform x:Name="rotateTransform"/>
    </Ellipse.RenderTransform>
</Ellipse>

您可以运行一个动画,例如随机旋转 0 到 3600 度之间的总角度,速度为每秒 1000 度:

var totalRotationAngle = rand.NextDouble() * 3600;

rotateTransform.BeginAnimation(RotateTransform.AngleProperty, new DoubleAnimation
{
    By = totalRotationAngle,
    Duration = TimeSpan.FromMilliseconds(totalRotationAngle)
});

【讨论】:

    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多