Avalonia学习(二十二)-等待

前端代码(主要是样式)


  

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

后台代码

using Avalonia;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
using System;
using System.Collections.Generic;
using System.Text;

namespace AvaloniaProgressRing
{
    public class ProgressRing : TemplatedControl
    {
        private const string LargeState = ":large";
        private const string SmallState = ":small";

        private const string InactiveState = ":inactive";
        private const string ActiveState = ":active";

        private double _maxSideLength = 10;
        private double _ellipseDiameter = 10;
        private Thickness _ellipseOffset = new Thickness(2);

        static ProgressRing()
        {
            //DefaultStyleKeyProperty.OverrideMetadata(typeof(ProgressRing),
            //    new FrameworkPropertyMetadata(typeof(ProgressRing)));
        }

        public ProgressRing()
        {
        }

        #region IsActive

        public bool IsActive
        {
            get => (bool)GetValue(IsActiveProperty);
            set => SetValue(IsActiveProperty, value);
        }


        public static readonly StyledProperty IsActiveProperty =
            AvaloniaProperty.Register(
                nameof(IsActive), 
                defaultValue: true);

        private static void OnIsActiveChanged(AvaloniaObject obj, bool arg2)
        {
            ((ProgressRing)obj).UpdateVisualStates();
        }

        public static readonly DirectProperty MaxSideLengthProperty =
            AvaloniaProperty.RegisterDirect(
               nameof(MaxSideLength),
               o => o.MaxSideLength);

        public double MaxSideLength
        {
            get { return _maxSideLength; }
            private set { SetAndRaise(MaxSideLengthProperty, ref _maxSideLength, value); }
        }

        public static readonly DirectProperty EllipseDiameterProperty =
            AvaloniaProperty.RegisterDirect(
               nameof(EllipseDiameter),
               o => o.EllipseDiameter);

        public double EllipseDiameter
        {
            get { return _ellipseDiameter; }
            private set { SetAndRaise(EllipseDiameterProperty, ref _ellipseDiameter, value); }
        }

        public static readonly DirectProperty EllipseOffsetProperty =
            AvaloniaProperty.RegisterDirect(
               nameof(EllipseOffset),
               o => o.EllipseOffset);

        public Thickness EllipseOffset
        {
            get { return _ellipseOffset; }
            private set { SetAndRaise(EllipseOffsetProperty, ref _ellipseOffset, value); }
        }

        #endregion


        protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
        {
            base.OnApplyTemplate(e);
            double maxSideLength = Math.Min(this.Width, this.Height);
            double ellipseDiameter = 0.1 * maxSideLength;
            if (maxSideLength <= 40)
            {
                ellipseDiameter += 1;
            }

            EllipseDiameter = ellipseDiameter;
            MaxSideLength = maxSideLength;
            EllipseOffset = new Thickness(0, maxSideLength / 2 - ellipseDiameter, 0, 0);
            UpdateVisualStates();
        }

        protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
        {
            base.OnPropertyChanged(change);

            if (change.Property == IsActiveProperty)
            {
                UpdateVisualStates();
            }
        }

        private void UpdateVisualStates()
        {
            PseudoClasses.Remove(ActiveState);
            PseudoClasses.Remove(InactiveState);
            PseudoClasses.Remove(SmallState);
            PseudoClasses.Remove(LargeState);
            PseudoClasses.Add(IsActive ? ActiveState : InactiveState);
            PseudoClasses.Add(_maxSideLength < 60 ? SmallState : LargeState);
        }
    }
}

运行效果

你可能感兴趣的:(学习,ui,Avalonia)