【问题标题】:WPF Resources Storyboard BindingWPF 资源故事板绑定
【发布时间】:2013-02-02 18:35:49
【问题描述】:

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

<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;

【问题讨论】:

    标签: c# wpf animation binding storyboard


    【解决方案1】:

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

    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-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2011-03-11
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多