簡體   English   中英

將C#動畫代碼轉換為情節提要

[英]Convert C# animation code to storyboard

我有以下可行的方法。 我想將其放在返回Storyboard的實用程序方法中。 我將其轉換為情節提要的所有嘗試均以失敗告終,並且我花費了大量時間進行研究。 我准備放棄,除非有人來救我。

這是我要轉換的代碼:

public override void Begin(FrameworkElement element, int duration)
{
    var transform = new ScaleTransform();
    element.LayoutTransform = transform;

    var animation = new DoubleAnimation
                        {
                            From = 1,
                            To = 0,
                            Duration = TimeSpan.FromMilliseconds(duration),
                            FillBehavior = FillBehavior.Stop,
                            EasingFunction = new QuinticEase { EasingMode = EasingMode.EaseIn }
                        };

    transform.BeginAnimation(ScaleTransform.ScaleXProperty, animation);
    transform.BeginAnimation(ScaleTransform.ScaleYProperty, animation);
}

因此,我想返回一個Storyboard,而不是兩個BeginAnimation()調用,所以我要做的就是調用Storyboard.Begin()。 我知道這樣做並不難,但我只是不明白。

謝謝。

編輯:為響應HB的建議,我嘗試了下面的代碼,仍然無法正常工作:

private static Storyboard CreateAnimationStoryboard(FrameworkElement element, int duration)
{
    var sb = new Storyboard();
    var scale = new ScaleTransform(1, 1);
    element.RenderTransform = scale;
    element.RegisterName("scale", scale);

    var animation = new DoubleAnimation
    {
        From = 1,
        To = 0,
        Duration = TimeSpan.FromMilliseconds(duration),
        FillBehavior = FillBehavior.Stop,
        EasingFunction = new QuinticEase { EasingMode = EasingMode.EaseIn }
    };
    sb.Children.Add(animation);

    Storyboard.SetTarget(animation, scale);
    Storyboard.SetTargetProperty(animation, new PropertyPath(ScaleTransform.ScaleXProperty));

    return sb;
}

我知道我只對X軸進行了動畫處理-只想先做點工作。

您將需要兩個動畫,然后使用SetTargetPropertySetTargetName將附加的Storyboard屬性設置為對正確對象上的正確屬性進行動畫SetTargetName

由於情節提要板的工作方式,您還需要設置一個名稱范圍( NameScope.SetNameScope ),注冊轉換的名稱,並使用包含元素重載調用StoryBoard.Begin

例如

NameScope.SetNameScope(element, new NameScope());

var transform = new ScaleTransform();
var transformName = "transform";
element.RegisterName(transformName, transform);
element.RenderTransform = transform;

var xAnimation = new DoubleAnimation(2, TimeSpan.FromSeconds(1));
var yAnimation = xAnimation.Clone();

var storyboard = new Storyboard()
{
    Children = { xAnimation, yAnimation }
};

Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(ScaleTransform.ScaleX)"));
Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(ScaleTransform.ScaleY)"));

Storyboard.SetTargetName(xAnimation, transformName);
Storyboard.SetTargetName(yAnimation, transformName);

storyboard.Begin(element);

我建議使用Expression Blend並從那里開始錄制,它應該在XAML中創建情節提要。 與其使用C#對其進行硬編碼,然后嘗試將其一一轉換為情節提要,這可能是一個容易發生的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM