パスマークアップを PathGeometry に変換

#wpdev_jp

パスの表現には パスマークアップがある。これを階層表現にすると複雑になる。さらに、オブジェクト表現にすると大変になる。

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

と、これは一緒。

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathFigure StartPoint="10,50">
        <LineSegment Point="200,70" />
      </PathFigure>
    </PathGeometry>
  </Path.Data>
</Path>

が、パスマークアップはXAMLの中でしか使えない。PathGeometryになればもう少し使い物になるのに。ということでその変換方法。

public static Geometry GetPathGeometry(String pathMarkUpString)
{
    string pathEnvelope = "<Geometry xmlns=\"https://schemas.microsoft.com/winfx/2006/xaml/presentation\">{0}</Geometry>";
    return XamlReader.Load(String.Format(pathEnvelope, pathMarkUpString)) as Geometry;
}