1#if !XAMARIN && !WINDOWS_UWP
11using System.Windows.Controls;
12using System.Windows.Media;
13using System.Windows.Threading;
14using System.ComponentModel;
15using System.Globalization;
22 [TemplatePart(Name =
"part1", Type = typeof(FrameworkElement))]
23 [TemplatePart(Name =
"part2", Type = typeof(FrameworkElement))]
24 [TemplatePart(Name =
"part3", Type = typeof(FrameworkElement))]
25 [TemplatePart(Name =
"part4", Type = typeof(FrameworkElement))]
26 [TemplatePart(Name =
"part5", Type = typeof(FrameworkElement))]
27 [TemplatePart(Name =
"part6", Type = typeof(FrameworkElement))]
28 [TemplatePart(Name =
"part7", Type = typeof(FrameworkElement))]
29 [TemplatePart(Name =
"part8", Type = typeof(FrameworkElement))]
30 [TemplateVisualState(Name =
"normal", GroupName =
"CommonStates")]
31 [TemplateVisualState(Name =
"state1", GroupName =
"CommonStates")]
32 [TemplateVisualState(Name =
"state2", GroupName =
"CommonStates")]
33 [TemplateVisualState(Name =
"state3", GroupName =
"CommonStates")]
34 [TemplateVisualState(Name =
"state4", GroupName =
"CommonStates")]
35 [TemplateVisualState(Name =
"state5", GroupName =
"CommonStates")]
36 [TemplateVisualState(Name =
"state6", GroupName =
"CommonStates")]
37 [TemplateVisualState(Name =
"state7", GroupName =
"CommonStates")]
38 [TemplateVisualState(Name =
"state8", GroupName =
"CommonStates")]
43 private const int NUM_STATES = 8;
47 #region Member fields and properties
49 private DispatcherTimer _timer;
50 private int _state = -1;
60 TimeSpan.FromMilliseconds(100),
63 var busyAnimation = (BusyAnimation)o;
64 busyAnimation.StepInterval = (TimeSpan)e.NewValue;
65 if (busyAnimation._timer != null)
66 busyAnimation._timer.Interval = busyAnimation.StepInterval;
77 new PropertyMetadata((o, e) => ((
BusyAnimation)o).SetupRunningState((
bool)e.NewValue)));
110 private void SetupRunningState(
bool isRunning)
135 SizeChanged += BusyAnimation_SizeChanged;
136 LayoutUpdated += BusyAnimation_LayoutUpdated;
143 private void StartTimer()
147 _timer =
new DispatcherTimer();
149 _timer.Tick += timer_Tick;
154 private void StopTimer()
159 _timer.Tick -= timer_Tick;
164 void timer_Tick(
object sender, EventArgs e)
167 if (_state >= NUM_STATES)
177 private void GoToState(
bool useTransitions)
181 VisualStateManager.GoToState(
this,
string.Format(
"state{0}", _state + 1), useTransitions);
185 VisualStateManager.GoToState(
this,
"normal", useTransitions);
193 void BusyAnimation_LayoutUpdated(
object sender, EventArgs e)
198 void BusyAnimation_SizeChanged(
object sender, SizeChangedEventArgs e)
203 private void ArrangeParts()
205 double width = ActualWidth;
206 double height = ActualHeight;
207 double scale = Math.Min(ActualWidth, ActualHeight);
208 double theta = (2.0 * Math.PI) / NUM_STATES;
210 for (
int n = 0; n < NUM_STATES; n++)
212 FrameworkElement item = (FrameworkElement)FindChild(
this,
"part" + (n + 1));
215 double itemTheta = theta * (double)n;
217 double itemScale = scale / 4.0;
218 double radius = (scale - itemScale) / 2.0;
220 double x = (radius * Math.Cos(itemTheta)) - (itemScale / 2) + (ActualWidth / 2);
221 double y = (radius * Math.Sin(itemTheta)) - (itemScale / 2) + (ActualHeight / 2);
223 item.Width = itemScale;
224 item.Height = itemScale;
225 item.SetValue(Canvas.LeftProperty, x);
226 item.SetValue(Canvas.TopProperty, y);
235 private DependencyObject FindChild(DependencyObject parent,
string name)
237 DependencyObject found =
null;
238 int count = VisualTreeHelper.GetChildrenCount(parent);
239 for (
int x = 0; x < count; x++)
241 DependencyObject child = VisualTreeHelper.GetChild(parent, x);
242 string childName = child.GetValue(FrameworkElement.NameProperty) as string;
243 if (childName == name)
248 else found = FindChild(child, name);
Displays a busy animation.
bool IsRunning
Gets or sets a property controlling whether the animation is running.
static readonly DependencyProperty IsRunningProperty
IsRunning property to control whether the animation is running.
BusyAnimation()
Creates an instance of the control.
TimeSpan StepInterval
Gets or sets a property controlling the speed of the animation.
static readonly DependencyProperty StepIntervalProperty
StepInterval property to control speed of animation.