집 >백엔드 개발 >C#.Net 튜토리얼 >WPF에서 GridLengthAnimation 사용자 정의에 대한 예제 튜토리얼
이 글에서는 주로 WPF에서 GridLengthAnimation을 사용자 정의하는 관련 정보를 자세히 소개합니다. 관심 있는 친구는
요구 사항
디자인
애니메이션 클래스 다이어그램에서 봅니다
디자인
애니메이션 클래스 다이어그램에서 AnimationTimeline 상속을 확인하고 해당 GetCurrentValuepublic class GridLengthAnimation : AnimationTimeline { /// <summary> /// Returns the type of object to animate /// </summary> public override Type TargetPropertyType => typeof(GridLength); /// <summary> /// Creates an instance of the animation object /// </summary> /// <returns>Returns the instance of the GridLengthAnimation</returns> protected override System.Windows.Freezable CreateInstanceCore() { return new GridLengthAnimation(); } /// <summary> /// Dependency property for the From property /// </summary> public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(GridLength), typeof(GridLengthAnimation)); /// <summary> /// CLR Wrapper for the From depenendency property /// </summary> public GridLength From { get { return (GridLength)GetValue(GridLengthAnimation.FromProperty); } set { SetValue(GridLengthAnimation.FromProperty, value); } } /// <summary> /// Dependency property for the To property /// </summary> public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(GridLength), typeof(GridLengthAnimation)); /// <summary> /// CLR Wrapper for the To property /// </summary> public GridLength To { get { return (GridLength)GetValue(GridLengthAnimation.ToProperty); } set { SetValue(GridLengthAnimation.ToProperty, value); } } /// <summary> /// Animates the grid let set /// </summary> /// <param name="defaultOriginValue">The original value to animate</param> /// <param name="defaultDestinationValue">The final value</param> /// <param name="animationClock">The animation clock (timer)</param> /// <returns>Returns the new grid length to set</returns> public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock) { double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value; double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value; if (fromVal > toVal) return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal, GridUnitType.Star); else return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal, GridUnitType.Star); }를 재정의합니다. 위에 표시된 대로 기본 애니메이션을 모방합니다. From 및 To를 구현하고
속성 을 GridLength로 정의합니다. 애니메이션이 실행되면 GetCurrentValue를 다시 작성하여 From/To 속성에 따라 연결합니다.
최적화
함수 f(t)라는 것을 알고 있습니다. 시간 함수 처리를 통해 애니메이션 전환을 덜 선형적으로 만듭니다.
/// <summary> /// The <see cref="EasingFunction" /> dependency property's name. /// </summary> public const string EasingFunctionPropertyName = "EasingFunction"; /// <summary> /// Gets or sets the value of the <see cref="EasingFunction" /> /// property. This is a dependency property. /// </summary> public IEasingFunction EasingFunction { get { return (IEasingFunction)GetValue(EasingFunctionProperty); } set { SetValue(EasingFunctionProperty, value); } } /// <summary> /// Identifies the <see cref="EasingFunction" /> dependency property. /// </summary> public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register( EasingFunctionPropertyName, typeof(IEasingFunction), typeof(GridLengthAnimation), new UIPropertyMetadata(null));이에 따라 GetCurrentValue 함수도 다시 작성해야 합니다.
public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock) { double fromVal = ((GridLength)GetValue(FromProperty)).Value; double toVal = ((GridLength)GetValue(ToProperty)).Value; //check that from was set from the caller //if (fromVal == 1) // //set the from as the actual value // fromVal = ((GridLength)defaultDestinationValue).Value; double progress = animationClock.CurrentProgress.Value; IEasingFunction easingFunction = EasingFunction; if (easingFunction != null) { progress = easingFunction.Ease(progress); } if (fromVal > toVal) return new GridLength((1 - progress) * (fromVal - toVal) + toVal, GridUnitType.Star); return new GridLength(progress * (toVal - fromVal) + fromVal, GridUnitType.Star); }
사용
<anim:GridLengthAnimation Storyboard.TargetProperty="Width" From="0" To="*" Duration="0:0:0.5"/>[관련 추천] 2.
C# WinForm의 단축키 사용자 정의 예, _PHP 튜토리얼
3. WeChat 공개 계정을 공유하여 사용자 정의 메뉴 개발 튜토리얼 예위 내용은 WPF에서 GridLengthAnimation 사용자 정의에 대한 예제 튜토리얼의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!