如何:在 PathGeometry 中创建 LineSegment
本示例演示如何创建线段。 若要创建线段,请使用 PathGeometry、PathFigure 和 LineSegment 类。
示例
下面的示例绘制一个从 (10,50) 到 (200,70) 的 LineSegment。 下图显示了最后得到的 LineSegment;其中添加了网格背景以显示坐标系。
从 (10,50) 绘制到 (200,700) 的 LineSegment
[xaml]
在Extensible Application Markup Language (XAML) 中,可以使用特性语法来描述路径。
<Path Stroke="Black" StrokeThickness="1"
Data="M 10,50 L 200,70" />
[xaml]
(请注意,此特性语法实际上会创建一个 StreamGeometry(PathGeometry 的轻量版本)。 有关更多信息,请参见路径标记语法页。)
在 XAML 中,还可以使用对象元素语法来绘制线段。 下面的示例与前面的 XAML 示例是等效的。
Dim myPathFigure As New PathFigure()
myPathFigure.StartPoint = New Point(10, 50)
Dim myLineSegment As New LineSegment()
myLineSegment.Point = New Point(200, 70)
Dim myPathSegmentCollection As New PathSegmentCollection()
myPathSegmentCollection.Add(myLineSegment)
myPathFigure.Segments = myPathSegmentCollection
Dim myPathFigureCollection As New PathFigureCollection()
myPathFigureCollection.Add(myPathFigure)
Dim myPathGeometry As New PathGeometry()
myPathGeometry.Figures = myPathFigureCollection
Dim myPath As New Path()
myPath.Stroke = Brushes.Black
myPath.StrokeThickness = 1
myPath.Data = myPathGeometry
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);
LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
myPathFigure.Segments = myPathSegmentCollection;
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="10,50">
<LineSegment Point="200,70" />
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
此示例摘自一个更大的示例;有关完整示例,请参见 Geometries Sample(几何图形示例)。