简体   繁体   中英

How do I add a random dispatcher timer for a lucky draw wheel(C#, XAML)

I have created a lucky draw wheel that is able to rotate when the form is loaded and spin with an acceleration and a deceleration when a button is clicked. However, I need to use a dispatcher timer instead(because it is a lucky draw right, I should not set a fix duration.) When clicked, it should spin between a duration and when it stops it will prompt a prize that should be extracted from a sqltable. The code that I currently have is for the load event and storyboard.

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>

I have implemented this code for a dispatcher timer, however i would like to do the storyboard action instead during the duration of the dispatcher timer.

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);
        }

Assuming that there is an Ellipse element in XAML

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

you could run an animation that for example randomly rotates by a total angle between 0 and 3600 degrees, with a speed of 1000 degrees per second:

var totalRotationAngle = rand.NextDouble() * 3600;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM