0

我有以下动画,它就像一个魅力:

<Window.Resources>
    <Namespace:MathConverter Core:Key="MathConverter"/>
    <Storyboard Core:Key="MyKey" Completed="OnCompleted">
        <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" BeginTime="00:00:00.0" By="135" Duration="00:00:0.2"/>
        <DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)" BeginTime="00:00:00.2" Duration="00:00:00.1" To="0">
            <DoubleAnimation.EasingFunction>
                <QuinticEase EasingMode="EaseInOut"/>
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>
</Window.Resources>

现在......我的布局是根据一些特定的基本值动态构建的。我希望我的动画以同样的方式表现。问题是,如果我按如下方式修改我的声明(MultiBinding Converter 的返回值绝对正确,我仔细检查了它)它就会停止正常工作:

<Storyboard Core:Key="MyKey" Completed="OnCompleted">
    <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" BeginTime="00:00:00.0" Duration="00:00:0.2">
        <DoubleAnimation.By>
            <MultiBinding Converter="{StaticResource MathConverter}" ConverterParameter="((x - y) / 2) + z + w">
                <Binding Source="{Core:Static Namespace:MyClass.RealHeight}"/>
                <Binding Source="{Core:Static Namespace:MyClass.RealWidth}"/>
                <Binding Source="{Core:Static Namespace:MyClass.MarginInner}"/>
                <Binding Source="{Core:Static Namespace:MyClass.RealWidth}"/>
            </MultiBinding>
        </DoubleAnimation.By>
    </DoubleAnimation>
    <DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)" BeginTime="00:00:00.2" Duration="00:00:00.1" To="0">
        <DoubleAnimation.EasingFunction>
            <QuinticEase EasingMode="EaseInOut"/>
        </DoubleAnimation.EasingFunction>
    </DoubleAnimation>
</Storyboard>

“By”值,在运行时调试时为空。为什么不应该在这里绑定工作?有什么解决方法吗?在后面的代码中手动设置该值当然可以......但是很好......

m_MyAnimation = (Storyboard)Resources["MyKey"];
((DoubleAnimation)m_MyAnimation.Children[0]).By = ((MyClass.RealHeight - MyClass.RealWidth) / 2D) + MyClass.MarginInner + MyClass.RealWidth;
4

1 回答 1

0

问题是我的转换器处理所有返回值的方式:

public Object Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture)
{
    try
    {
        String text = parameter.ToString();
        IExpression expression = null;

        if (!m_Expressions.TryGetValue(text, out expression))
            m_Expressions[text] = expression = m_Parser.Parse(text);

        Decimal result = expression.Eval(values);

        if (targetType == typeof(Decimal))
            return result;

        if (targetType == typeof(Double))
            return (Double)result;

        if (targetType == typeof(Int32))
            return (Int32)result;

        if (targetType == typeof(Int64))
            return (Int64)result;

        if (targetType == typeof(String))
            return result.ToString();
    }
    catch { }

    return DependencyProperty.UnsetValue;
}

那种动画值是可以为空的类型......所以我改变了我的方法以使一切都像魅力一样工作:

public Object Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture)
{
    try
    {
        String text = parameter.ToString();
        IExpression expression = null;

        if (!m_Expressions.TryGetValue(text, out expression))
            m_Expressions[text] = expression = m_Parser.Parse(text);

        Decimal result = expression.Eval(values);

        if (targetType.IsGenericType && (targetType.GetGenericTypeDefinition() == typeof(Nullable<>)))
            targetType = Nullable.GetUnderlyingType(targetType);

        if (targetType == typeof(Decimal))
            return result;

        if (targetType == typeof(Double))
            return (Double)result;

        if (targetType == typeof(Int32))
            return (Int32)result;

        if (targetType == typeof(Int64))
            return (Int64)result;

        if (targetType == typeof(String))
            return result.ToString();
    }
    catch { }

    return DependencyProperty.UnsetValue;
}
于 2013-02-18T13:21:11.377 回答