一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - WPF中自定義GridLengthAnimation

WPF中自定義GridLengthAnimation

2020-05-05 15:46HelyCheng ASP.NET教程

這篇文章主要為大家詳細介紹了WPF中自定義GridLengthAnimation的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

需求

我們想在編輯一個列表中某一個條目時,將編輯的詳情內容也放置當前面,比如右側。

可以通過將一個Grid,分成兩個Cloumn,動態調整兩個Cloumn的Width,就可以實現這個需求。

我們知道,Clomun的Width是個,而默認的動畫沒有這樣子的。我們就需要自己實現這樣一人動畫。

設計
我們從Animation的類圖上看到

WPF中自定義GridLengthAnimation

我們可以從需求

我們想在編輯一個列表中某一個條目時,將編輯的詳情內容也放置當前面,比如右側。

可以通過將一個Grid,分成兩個Cloumn,動態調整兩個Cloumn的Width,就可以實現這個需求。

我們知道,Clomun的Width是個GridLength,而默認的動畫沒有這樣子的。我們就需要自己實現這樣一人動畫。

設計

我們從Animation的類圖上看到AnimationTimeline繼承,重寫其GetCurrentValue

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public 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屬性相關聯。

優化

通過以上代碼,我們實現了在GridLength變化時,實現動畫。但是,試用后我們發現,動畫,有點太線性。這個時候,怎么辦?

可以通過引入EasingFunction來實現。我們知道EasingFunction其實就是一個與時間t有關的時間函數f(t).通過時間函數的處理,我們使動畫過渡不要那么線性。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/// <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函數。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
    }

使用

?
1
<anim:GridLengthAnimation Storyboard.TargetProperty="Width" From="0" To="*" Duration="0:0:0.5"/>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久全国免费久久青青小草 | yjsp妖精视频在线观看免费 | 亚洲爱v | 欧美国产精品 | gay台湾无套男同志可播放 | 国产香蕉视频在线观看 | 国产一级片免费观看 | 欧美老肥妇bbb| 国产一区二区免费不卡在线播放 | 欧美在线视频一区在线观看 | 久久AV喷吹AV高潮欧美 | 亚洲欧美专区精品久久 | 国产一区视频在线免费观看 | avove本人照片 | 国产小青蛙 | 火影忍者小南裸羞羞漫画 | 亚洲国产三级在线观看 | 国产视频99 | 亚洲国产高清一区二区三区 | 亚州日韩精品AV片无码中文 | 亚洲 欧美 中文 日韩欧美 | 亚洲欧美日韩另类在线 | chinses台湾男同志hd | 日本妇人成熟免费观看18 | 国产成人综合一区精品 | a级成人毛片免费图片 | 欧美日韩亚洲一区二区三区在线观看 | 穆挂英风流艳史小说 | 特级淫片欧美高清视频蜜桃 | 色婷婷婷婷 | 好爽好粗 | 亚洲网色 | 无码国产成人777爽死在线观看 | 99久久久久久久 | 性猛交娇小69hd | 日本人护士免费xxxx视频 | 国产亚洲人成网站在线观看不卡 | 久久精品亚洲精品国产欧美 | 99久久国产视频 | 亚洲AV 日韩 国产 有码 | 楚乔传第二部免费观看全集完整版 |