需要帮助才能将XAML转换为C#代码

时间:2009-08-26 15:09:29

标签: wpf

这是一段绘制Bezier代码的XAML脚本。我需要的是一个纯粹的C#代码,它可以实现相同的结果但不使用XAML。

任何人都可以帮助将其转换为C#代码吗?

提前致谢!

麦克

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathGeometry.Figures>
        <PathFigureCollection>
          <PathFigure StartPoint="10,100">
            <PathFigure.Segments>
              <PathSegmentCollection>
                <BezierSegment Point1="100,0" Point2="200,200" Point3="300,100" />
              </PathSegmentCollection>
            </PathFigure.Segments>
          </PathFigure>
        </PathFigureCollection>
      </PathGeometry.Figures>
    </PathGeometry>
  </Path.Data>
</Path>

2 个答案:

答案 0 :(得分:4)

我测试了这段代码并且有效。

        Path path = new Path();
        path.Stroke = new SolidColorBrush(Colors.Black);
        path.StrokeThickness = 10;
        PathGeometry pg = new PathGeometry();
        PathFigureCollection pfc = new PathFigureCollection();
        PathFigure fig = new PathFigure();
        PathSegmentCollection psc = new PathSegmentCollection();
        BezierSegment bs1 = new BezierSegment(new Point(100, 0), new Point(200, 200), new Point(300, 100), true);
        psc.Add(bs1);
        fig.Segments = psc;
        pfc.Add(fig);
        pg.Figures = pfc;
        path.Data = pg;

答案 1 :(得分:0)

Path path = new Path();
path.Stroke = Brushes.Black;
path.StrokeThickness = 1;
PathGeometry pg = new PathGeometry();
PathFigureCollection pfc = new PathFigureCollection();
PathFigure fig = new PathFigure();
fig.StartPoint = new Point(10,100);
PathSegmentCollection psc = new PathSegmentCollection();
BezierSegment bs1 = new BezierSegment(new Point(100, 0), new Point(200, 200), new Point(300, 100), true);
psc.Add(bs1);
fig.Segments = psc;
pfc.Add(fig);
pg.Figures = pfc;
path.Data = pg;
canvas.Children.Add(path);