簡體   English   中英

將XAML轉換為C#代碼

[英]Convert xaml to C# code

我是在wpf中使用path的新手,而且我不知道如何將xaml代碼段轉換為C#代碼。 有人可以幫我嗎? 我引用了xaml代碼,然后嘗試將其轉換。 C#代碼缺少什么? 我想問的另一件事是,網格是否足以在窗口中顯示路徑。

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

我的C#代碼:

Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = new PathFigureCollection();

PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 100);
myPathFigure.Segments = new PathSegmentCollection();
QuadraticBezierSegment theSegment = new QuadraticBezierSegment();
theSegment.Point1 = new Point(200, 200);
theSegment.Point2 = new Point(100, 300);
myPathFigure.Segments.Add(theSegment);
myPathGeometry.Figures.Add(myPathFigure);

您必須在末尾添加以下行,

myPath.Data = myPathGeometry;

你應該添加X:命名你的<Grid><Grid x:Name='myGrid'>

再加一行

myGrid.Children.Add(myPath);

您的C#代碼看起來很像WPF標記。 只需將路徑添加到要顯示它的控件即可。

var myPath = new Path
{
    Stroke = Brushes.Black,
    StrokeThickness = 1.0,
    Data = new PathGeometry
    {
        Figures = new PathFigureCollection
        {
            new PathFigure
            {
                StartPoint = new Point(10, 100),
                Segments = new PathSegmentCollection
                {
                    new QuadraticBezierSegment
                    {
                        Point1 = new Point(200, 200),
                        Point2 = new Point(300, 100),
                    },
                },
            },
        },
    },
};
myGrid.Children.Add(myPath);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM