【问题标题】:Storyboard.SetTargetProperty not working under Windows phone 8.1?Storyboard.SetTargetProperty 在 Windows phone 8.1 下不起作用?
【发布时间】:2015-02-23 12:12:40
【问题描述】:

我从gesture example app I found online 为Windows Phone 7 使用了以下扩展方法。我正在尝试将其迁移到我的应用程序的8.1,但遇到了Storyboard.SetTargetProperty 方法调用的问题。在 Windows 8.1 中似乎是 this signature is not supported

总的来说,我对 Windows Phone 开发相当陌生,不确定应该使用什么来代替它,有人可以指出 API 中的一个方法,它提供了它试图实现的目标吗?看来签名现在需要一个字符串,以前允许使用 PropertyPath。我不确定我需要做什么才能将 DP 属性转换为代表它的字符串,设置为 DP 属性名称的字符串是否有效?

public static void Animate(this DependencyObject target, double from, double to,
                        object propertyPath, int duration, int startTime,
                        Action completed = null)
{
    var db = new DoubleAnimation();
    db.To = to;
    db.From = from;
    db.EasingFunction = new SineEase();
    db.Duration = TimeSpan.FromMilliseconds(duration);
    Storyboard.SetTarget(db, target);

    // Compiler error: This method signature is no longer supported.
    Storyboard.SetTargetProperty(db, new PropertyPath(propertyPath));

    var sb = new Storyboard();
    sb.BeginTime = TimeSpan.FromMilliseconds(startTime);

    if (completed != null)
    {
        sb.Completed += (s, e) => completed();
    }

    sb.Children.Add(db);
    sb.Begin();
}

这样调用

TranslateTransform elementTrans = new TranslateTransform();
elementTrans.Animate(0, elementOffset, TranslateTransform.YProperty, 200, startTime, null, action);

不确定如何将TranslateTransform.YProperty 转换为可以传递给SetTargetProperty 调用的字符串。

编辑

have found some documentation on MSDN 展示了如何通过字符串传递属性名称。我认为我没有正确提供它,因为我得到一个运行时异常,它找不到提供的属性。

        transform.Animate(transform.X, 0, "(FrameworkElement.RenderTransform).(TranslateTransform.XProperty)", 300, 0, new BounceEase()
        {
            Bounciness = 5,
            Bounces = 2
        });

请注意,我将方法签名更改为接受 propertyPath 的字符串而不是对象,但这里没有反映这一点,因为我想将原始源代码保持原样。

RenderTransform 设置为 TranslateTransform,如下所示:

    public static void SetHorizontalOffset(this FrameworkElement element, double offset)
    {
        var transform = new TranslateTransform { X = offset };
        element.RenderTransform = transform;
        element.Tag = new Offset { Value = offset, Transform = transform };
    }

【问题讨论】:

    标签: c# animation storyboard windows-phone-8.1


    【解决方案1】:

    您无需指定完整的属性路径。只需使用X 而不是整个(FrameworkElement.RenderTransform).(TranslateTransform.XProperty)

    例如

    transform.Animate(transform.X, 0, "X", 300, 0, new BounceEase()
    {
        Bounciness = 5,
        Bounces = 2
    });
    

    【讨论】:

    • 不用担心,还要注意,TranslateTransform.X 也可以。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多