namespace Melskin.Controls; using System.Windows; /// /// A spinner for displaying loading state of a page or a section. /// [TemplateVisualState(Name = "Spun", GroupName = "SpinStates")] [TemplateVisualState(Name = "Unspun", GroupName = "SpinStates")] public class Spin : ContentControl { #region Properties /// /// 获取或设置指示器属性的依赖属性。此属性用于定义旋转控件中使用的指示器元素。 /// public static readonly DependencyProperty IndicatorProperty = DependencyProperty.Register( nameof(Indicator), typeof(UIElement), typeof(Spin), new PropertyMetadata(null, OnSpinningChanged)); /// /// 获取或设置指示器元素。 /// public UIElement Indicator { get => (UIElement)GetValue(IndicatorProperty); set => SetValue(IndicatorProperty, value); } /// /// 获取或设置一个布尔值,指示旋转控件是否处于旋转状态。 /// public static readonly DependencyProperty SpinningProperty = DependencyProperty.Register( nameof(Spinning), typeof(bool), typeof(Spin), new PropertyMetadata(true, OnSpinningChanged)); /// /// 获取或设置是否正在旋转。 /// public bool Spinning { get => (bool)GetValue(SpinningProperty); set => SetValue(SpinningProperty, value); } private static void OnSpinningChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as Spin)?.GoToSpinState(); } private void GoToSpinState() { VisualStateManager.GoToState(this, Spinning && null == Indicator ? "Spun" : "Unspun", true); } /// /// 获取或设置提示文本。 /// public static readonly DependencyProperty TipProperty = DependencyProperty.Register(nameof(Tip), typeof(string), typeof(Spin), new PropertyMetadata(string.Empty)); /// /// 获取或设置提示文本。 /// public string Tip { get => (string)GetValue(TipProperty); set => SetValue(TipProperty, value); } #endregion #region Constructors static Spin() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Spin), new FrameworkPropertyMetadata(typeof(Spin))); } #endregion #region Overrides /// public override void OnApplyTemplate() { base.OnApplyTemplate(); GoToSpinState(); } #endregion }