using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;

namespace ControlExplorer.Controls.Gauge
{
    public partial class LinearGauge : UserControl
    {
        Storyboard storyboard = new Storyboard();
        DoubleAnimation animation = new DoubleAnimation();
        Random r = new Random();

        public LinearGauge()
        {
            InitializeComponent();

            Loaded += OnLoaded;
            Unloaded += OnUnloaded;
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            //Applies a random animation to show a more lively sample.
            animation.Duration = TimeSpan.FromSeconds(1);
            animation.EnableDependentAnimation = true;
            animation.EasingFunction = new QuadraticEase() { EasingMode = EasingMode.EaseInOut };
            Storyboard.SetTarget(animation, myGauge);
            Storyboard.SetTargetProperty(animation, "(c1Gaugeauge.Value)");
            storyboard.Children.Add(animation);
            storyboard.Completed += storyboard_Completed;
            storyboard.Begin();
        }

        void OnUnloaded(object sender, RoutedEventArgs e)
        {
            // stop storyboard at unloading
            storyboard.Completed -= storyboard_Completed;
            storyboard.Stop();
            storyboard.Children.Clear();
        }
        void storyboard_Completed(object sender, object e)
        {
            animation.From = animation.To;
            animation.To = r.NextDouble() * 100;
            storyboard.Begin();
        }
    }
}
