从代码中旋转对象

本文关键字:对象 旋转 代码 | 更新日期: 2023-09-27 17:50:13

我想用c#代码旋转一个在XAML中制作的多边形,但是我被困在Propertypath上。有人知道我应该用什么代替吗?

这是我现在的c#代码:

public void Rotate()
{
    Storyboard rotate = new Storyboard();
    DoubleAnimation myDoubleAnimation = new DoubleAnimation();
    myDoubleAnimation.From = 0;
    myDoubleAnimation.To = 360;
    myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
    Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(Polygon.RenderTransform).(RotateTransform.Angle)"));
    Storyboard.SetTargetName(myDoubleAnimation, Arrowhead.Name);
    rotate.Children.Add(myDoubleAnimation);
    rotate.Begin(Arrowhead);
}

这是我要在XAML中旋转的多边形(三角形):

                <Polygon Fill="#41b1ff"
                 Stroke="Gray"
                 StrokeThickness="2"
                 Points="80,60,100,40,100,40,120,60" 
                 Grid.ColumnSpan="3"
                 Grid.RowSpan="3"
                 Name="Arrowhead"
                     />

从代码中旋转对象

您的代码似乎工作良好。你需要添加你想要修改的转换:

<Polygon
        Fill="#41b1ff"
        Stroke="Gray"
        StrokeThickness="2"
        Points="80,60,100,40,100,40,120,60"
        Name="Arrowhead">
    <Polygon.RenderTransform>
        <RotateTransform/>
    </Polygon.RenderTransform>
</Polygon>