形状不遵循指定的路径

时间:2015-03-23 19:01:54

标签: c# wpf xaml

我遇到路径形状的一些问题,我希望MyCircle遵循我创建的路径。当我运行代码时,形状不遵循我想要的路径,它偏离中心。我不确定是什么问题。

这是一些生成带有路径和某些形状的画布的XAML代码。

<Window x:Class="TestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="480" Width="640">
<Canvas Margin="5">
    <Path HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="MyPathStroke" Stroke="Black" Canvas.Left="100" Canvas.Top="50">
        <Path.Data>
            <PathGeometry x:Name="MyPath" Figures="M421.9,110 C421.9,170.47518 327.5664,219.5 211.2,219.5 C94.833603,219.5 0.5,170.47518 0.5,110 C0.5,49.52482 94.833603,0.5 211.2,0.5 C327.5664,0.5 421.9,49.52482 421.9,110 Z" />
        </Path.Data>
    </Path>
    <Path Stroke="Black" StrokeThickness="3" Fill="Red" >
        <Path.Data>
            <EllipseGeometry 
                x:Name="MyCircle"
                Center="110,421.9" 
                RadiusX="10" 
                RadiusY="10" />
        </Path.Data>
    </Path>
    <Ellipse x:Name="MyQueue" Fill="#FFF4F4F5" Height="80" Width="80" Stroke="Black" Canvas.Left="270" Canvas.Top="10" />
    <Rectangle x:Name="MyActivity" Fill="#FFF4F4F5" Height="50" Width="100" Stroke="Black" Canvas.Left="260" Canvas.Top="240" />
</Canvas>
</Window>

这是我的代码,用于将圆圈指定给路径。

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace TestApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();          

            PointAnimationUsingPath pa = new PointAnimationUsingPath();

            pa.PathGeometry = MyPath;

            pa.Duration = TimeSpan.FromSeconds(5);

            pa.RepeatBehavior = RepeatBehavior.Forever;

            MyCircle.BeginAnimation(EllipseGeometry.CenterProperty, pa);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

路径的定位(Canvas Top / Left)可能适合您:

<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="480" Width="640">
<Canvas Margin="5">
    <Path HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="MyPathStroke" Stroke="Blue" Canvas.Left="100" Canvas.Top="50">
        <Path.Data>
            <PathGeometry x:Name="MyPath" Figures="M421.9,110 C421.9,170.47518 327.5664,219.5 211.2,219.5 C94.833603,219.5 0.5,170.47518 0.5,110 C0.5,49.52482 94.833603,0.5 211.2,0.5 C327.5664,0.5 421.9,49.52482 421.9,110 Z" />
        </Path.Data>
    </Path>
    <Path Stroke="Black" StrokeThickness="3" Fill="Red" Canvas.Top="50" Canvas.Left="100">
        <Path.Data>
            <EllipseGeometry 
            x:Name="MyCircle"
            RadiusX="10" 
            RadiusY="10" />
        </Path.Data>
    </Path>
    <Ellipse x:Name="MyQueue" Fill="#FFF4F4F5" Height="80" Width="80" Stroke="Black" Canvas.Left="270" Canvas.Top="10" />
    <Rectangle x:Name="MyActivity" Fill="#FFF4F4F5" Height="50" Width="100" Stroke="Black" Canvas.Left="260" Canvas.Top="240" />
</Canvas>