没有使用触发器的PointAnimation

本文关键字:PointAnimation 触发器 | 更新日期: 2023-09-27 18:06:29

我想在矩形上使用pointanimation。我在WPF工作。如果我创建storyboard,只写DoubleAnimation works。如果我添加了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>

没有使用触发器的PointAnimation

你的故事板的问题是没有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);
    }
}
在上面的代码中,我正在操作画布。左侧和画布。属性更改处理程序 中的矩形顶部

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

我为Location做了一个例子,你也可以通过一些计算来实现居中