Share via


操作說明:在 PathGeometry 中建立 LineSegment

這個範例會示範如何建立線段 。 若要建立線段,請使用 PathGeometryPathFigureLineSegment 類別。

範例

下列範例會從 (10, 50) 到 (200, 70) 繪製 LineSegment 。 下圖顯示產生的 LineSegment ;已新增格線背景以顯示座標系統。

A LineSegment in a PathFigure 從 (10,50) 到 (200,70) 繪製的線條

在 Extensible Application Markup Language (XAML)中,您可以使用屬性語法來描述路徑。

<Path Stroke="Black" StrokeThickness="1"
  Data="M 10,50 L 200,70" />

(請注意,這個屬性語法實際上會建立 、 StreamGeometry 較輕量版本的 PathGeometry 。如需詳細資訊,請參閱 路徑標記語法 頁面。

在 XAML 中,您也可以使用物件元素語法來繪製線條區段。 下列相當於先前的 XAML 範例。

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathFigure StartPoint="10,50">
        <LineSegment Point="200,70" />
      </PathFigure>
    </PathGeometry>
  </Path.Data>
</Path>
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;
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

這個範例屬於較大型的範例;如需完整範例,請參閱幾何範例

另請參閱