Udostępnij za pośrednictwem


Jak utworzyć obiekt LineSegment w PathGeometry

W tym przykładzie pokazano, jak utworzyć segment wiersza. Aby utworzyć segment wiersza, użyj PathGeometryklas , PathFigurei LineSegment .

Przykład

W poniższych przykładach przedstawiono wartości LineSegment z (10, 50) do (200, 70). Na poniższej ilustracji przedstawiono wynikowe LineSegment; dodano tło siatki w celu pokazania układu współrzędnych.

A LineSegment in a PathFigure LiniaSegment pobierany z (10 50) do (200,70)

W języku Extensible Application Markup Language (XAML) można użyć składni atrybutów do opisania ścieżki.

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

(Należy pamiętać, że ta składnia atrybutu faktycznie tworzy StreamGeometry, lżejszą wersję elementu PathGeometry. Aby uzyskać więcej informacji, zobacz stronę Składnia znaczników ścieżki .

W języku XAML można również narysować segment wiersza przy użyciu składni elementu obiektu. Poniższy kod jest odpowiednikiem poprzedniego przykładu 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

Ten przykład jest częścią większej próbki; Aby zapoznać się z kompletnym przykładem, zobacz Przykład Geometries.

Zobacz też