如何在C#中绘制此WPF路径

时间:2012-04-16 08:01:55

标签: c# wpf pathgeometry figures

我一直在努力将这段WPF代码转换为C#。我对WPF比较陌生,我真的希望有人能在这里帮助我:)

    <Path Fill="Blue" Margin="15,15,15,15">
        <Path.Data>
            <EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
                Center="10,100" RadiusX="15" RadiusY="15" />
        </Path.Data>
        <Path.Triggers>
            <EventTrigger RoutedEvent="Path.Loaded">
                <BeginStoryboard Name="MyBeginStoryboard">
                    <Storyboard>

                        <!-- Animates the ellipse along the path. -->
                        <PointAnimationUsingPath
                            Storyboard.TargetName="MyAnimatedEllipseGeometry"
                            Storyboard.TargetProperty="Center"
                            Duration="0:0:5" 
                            RepeatBehavior="Forever" >
                            <PointAnimationUsingPath.PathGeometry>
                                <PathGeometry 
                                    Figures="M 10,100 C 35,0 135,0 160,100 135,0 35,0 10,100"
                                />
                            </PointAnimationUsingPath.PathGeometry>
                        </PointAnimationUsingPath>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Path.Triggers>
    </Path>
</Canvas>

我似乎相处得很好,直到我到达PathGeometry数字......如果有人能为我提供这个WPF代码的C#代码片段,我们将不胜感激!

2 个答案:

答案 0 :(得分:3)

我建议您阅读以下msdn article

"M 10,100 C 35,0 135,0 160,100 135,0 35,0 10,100"

意味着以下:从(10,100)开始。添加以10,100结尾的“立方贝塞尔曲线”,控制点(35,0),(135,0),(160,100),(135,0),(35,0)。

据我了解。这样的文本应该很容易呈现到PathSegmentCollection中。

答案 1 :(得分:1)

您正在寻找:PointAnimationUsingPath.PathGeometry。有一个如何解决问题的详细示例,它显示了如何使用PointAnimationUsingPath对象沿着弯曲路径为Point设置动画。对于complete sample;如果您不想使用Storyboard:How to: Animate a Property Without Using a Storyboard。请记住,MSDN中存有示例和详细explanations