Click here to Skip to main content
15,881,281 members
Articles / Desktop Programming / WPF

Commands in MVVM

Rate me:
Please Sign up or sign in to vote.
4.97/5 (107 votes)
3 Dec 2012CPOL15 min read 509.6K   16.9K   279  
A consistent approach to Commands, Asynchronous Commands, and Events-to-Commands for WPF, Silverlight, and WP7.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Apex.Controls
{
    /// <summary>
    /// Provides a circular progress bar
    /// </summary>
  public partial class CircularProgressBar : UserControl
  {
    public CircularProgressBar()
    {
      InitializeComponent();

      animationTimer = new DispatcherTimer(
          DispatcherPriority.ContextIdle, Dispatcher);
      animationTimer.Interval = new TimeSpan(0, 0, 0, 0, 75);
    }

    private readonly DispatcherTimer animationTimer;
      
    private void Start()
    {
      animationTimer.Tick += HandleAnimationTick;
      animationTimer.Start();
    }

    private void Stop()
    {
      animationTimer.Stop();
      animationTimer.Tick -= HandleAnimationTick;
    }

    private void HandleAnimationTick(object sender, EventArgs e)
    {
      SpinnerRotate.Angle = (SpinnerRotate.Angle + 36) % 360;
    }

    private void HandleLoaded(object sender, RoutedEventArgs e)
    {
      const double offset = Math.PI;
      const double step = Math.PI * 2 / 10.0;

      SetPosition(C0, offset, 0.0, step);
      SetPosition(C1, offset, 1.0, step);
      SetPosition(C2, offset, 2.0, step);
      SetPosition(C3, offset, 3.0, step);
      SetPosition(C4, offset, 4.0, step);
      SetPosition(C5, offset, 5.0, step);
      SetPosition(C6, offset, 6.0, step);
      SetPosition(C7, offset, 7.0, step);
      SetPosition(C8, offset, 8.0, step);
    }

    private void SetPosition(Ellipse ellipse, double offset,
        double posOffSet, double step)
    {
      ellipse.SetValue(Canvas.LeftProperty, 50.0
          + Math.Sin(offset + posOffSet * step) * 50.0);

      ellipse.SetValue(Canvas.TopProperty, 50
          + Math.Cos(offset + posOffSet * step) * 50.0);
    }

    private void HandleUnloaded(object sender, RoutedEventArgs e)
    {
      Stop();
    }

    private void HandleVisibleChanged(object sender,
        DependencyPropertyChangedEventArgs e)
    {
      bool isVisible = (bool)e.NewValue;

      if (isVisible)
        Start();
      else
        Stop();
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
    }

    public static DependencyProperty ProgressTextProperty = DependencyProperty.Register("ProgressText", typeof(string), typeof(CircularProgressBar));

    public string ProgressText
    {
      get { return (string)GetValue(ProgressTextProperty); }
      set { SetValue(ProgressTextProperty, value); }
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions