PointAnimation没有使用触发器

时间:2014-06-04 06:45:59

标签: c# wpf animation

我想在矩形上使用pointanimation。我在WPF工作。如果我创建故事板并且只写DoubleAnimation工作。如果我添加PointAnimation系统崩溃并写信给我。

在类型' example.MainWindow'上调用构造函数。匹配指定的绑定约束会引发异常。'

如果没有触发器可以使用这个选项吗?为什么这不起作用?

<Window.Resources>
  <Storyboard x:Key="move">
    <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="Width" To="100" Duration="0:0:2"></DoubleAnimation>
    <PointAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="Center" To="100,100" Duration="0:0:2"></PointAnimation>
  </Storyboard>    
</Window.Resources>

1 个答案:

答案 0 :(得分:1)

故事板的问题是没有{point}类型的Center属性可以设置动画。

背后的实际错误

The invocation of the constructor on type 'example.MainWindow' that matches the specified binding constraints threw an exception.'

Cannot resolve all property references in the property path 'Center'. Verify that applicable objects support the properties.

下面的

是使用Canvas.LeftCanvas.Top来实现类似行为的示例

    <Storyboard x:Key="move">
        <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="Width" To="100" Duration="0:0:2"></DoubleAnimation>
        <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="(Canvas.Left)" To="100" Duration="0:0:2"></DoubleAnimation>
        <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="(Canvas.Top)" To="100" Duration="0:0:2"></DoubleAnimation>
    </Storyboard>

使用PointAnimation示例设置位置

定义一个具有附加属性的类来保存值

class RectangleHelper : DependencyObject
{
    public static Point GetLocation(DependencyObject obj)
    {
        return (Point)obj.GetValue(LocationProperty);
    }

    public static void SetLocation(DependencyObject obj, Point value)
    {
        obj.SetValue(LocationProperty, value);
    }

    // Using a DependencyProperty as the backing store for Location.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LocationProperty =
        DependencyProperty.RegisterAttached("Location", typeof(Point), typeof(RectangleHelper), new PropertyMetadata(new Point(), OnLocationChanged));


    private static void OnLocationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Point newValue = (Point)e.NewValue;

        d.SetValue(Canvas.LeftProperty, newValue.X);
        d.SetValue(Canvas.TopProperty, newValue.Y);
    }
}

在上面的代码中我在属性更改处理程序中操作矩形上的Canvas.Left和Canvas.Top

然后xaml as

<Storyboard x:Key="move">
    <DoubleAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="Width" From="1" To="100" Duration="0:0:2"></DoubleAnimation>
    <PointAnimation Storyboard.TargetName="rec" Storyboard.TargetProperty="(l:RectangleHelper.Location)" To="100,100" Duration="0:0:2"></PointAnimation>
</Storyboard>

我为“位置”做了示例,您也可以通过一些计算来实现中心。