System.Drawing.Drawing2D.GraphicsPath.AddArc 在 XAML UWP 中等效

Equivalent for System.Drawing.Drawing2D.GraphicsPath.AddArc in XAML UWP

System.Drawing.Drawing2D.GraphicsPath.AddArc 在 Xaml UWP 渲染中是否有任何等价物?

据我所知,我已经在 XAML UWP 中尝试了以下用例。

        Path path = new Path();
        PathGeometry pathGeometry = new PathGeometry();
        PathFigure pathFigure = new PathFigure();
        ArcSegment arcSegment = new ArcSegment();
        arcSegment.IsLargeArc = true;
        arcSegment.Size = new Windows.Foundation.Size(100, 100);
        arcSegment.Point = new Windows.Foundation.Point(100, 100);
        arcSegment.RotationAngle = 180;
        arcSegment.SweepDirection = SweepDirection.Clockwise;
        pathFigure.StartPoint = new Windows.Foundation.Point(100, 100);
        pathFigure.Segments.Add(arcSegment);
        path.Data = pathGeometry;
        path.Fill = new SolidColorBrush(Colors.Red);
        path.StrokeThickness = 2;
        pathGeometry.Figures.Add(pathFigure);

但是上面不是100%等同于System.Drawing.Drawing2D.GraphicsPath.AddArc(Rectangle, Single, Single)

is there any equivalent for System.Drawing.Drawing2D.GraphicsPath.AddArc, in Xaml UWP rendering ?

在经典的 windows 表单应用程序中,我们有 System.Drawing.Drawing2D namespace for drawing graphics. But this namespace is not supported in UWP app. UWP app has its own APIs for drawing graphics. You should be able to draw shapes by APIs under Windows.UI.Xaml.Shapes namespace. More details about how to draw shapes on uwp app please reference this article.

方法System.Drawing.Drawing2D.GraphicsPath.AddArc实际上是用来画弧线的,你已经在uwp应用程序中找到了你可以使用添加ArcSegment来代替。

But the above is not 100% equivalent to System.Drawing.Drawing2D.GraphicsPath.AddArc(Rectangle, Single, Single)

UWP 应用程序有自己的绘图 API,它可能没有与 windows 形式具有相同方法名称和相同参数的方法。但是你应该可以使用uwp中的其他方法来实现同样的效果。例如,AddArc 方法有三个参数,Rectangle 用于定义 x 直径和 y 直径以及形状位置,而 startAnglesweepAngle 用于定义开始和结束角度。在uwp app中,ArcSegment可以通过size属性定义圆弧x-radius和y-radius,点定义起点和终点位置。

比如在一个windows的表格中,我们可以用下面的代码画一个圆弧;

 protected override void OnPaint(PaintEventArgs e)
 {
     base.OnPaint(e);
     // Create a GraphicsPath object.
     GraphicsPath myPath = new GraphicsPath();
     // Set up and call AddArc, and close the figure.
     Rectangle rect = new Rectangle(20, 20, 50, 100);
     myPath.StartFigure();
     myPath.AddArc(rect, 0, 180);
     myPath.CloseFigure();
     // Draw the path to screen.
     e.Graphics.DrawPath(new Pen(Color.Red, 3), myPath);
 }

在 uwp 应用程序中,您可以通过以下代码在 xaml 中执行相同的操作:

 <Path  StrokeThickness="2"  Margin="40,40,0,0" Stroke="Red">
     <Path.Data>
         <PathGeometry>
             <PathFigure IsClosed="True">
                 <PathFigure.Segments>
                     <PathSegmentCollection>   
                         <ArcSegment  Size="25,50" Point="50,0"  IsLargeArc="True"  />  
                     </PathSegmentCollection>
                 </PathFigure.Segments>
             </PathFigure>                 
         </PathGeometry>
     </Path.Data>
 </Path>

或后面的代码:

 private void btnCreatepath_Click(object sender, RoutedEventArgs e)
 {
     Path path = new Path();
     PathGeometry pathGeometry = new PathGeometry();
     PathFigure pathFigure = new PathFigure();
     ArcSegment arcSegment = new ArcSegment();
     arcSegment.IsLargeArc = true;
     arcSegment.Size = new Windows.Foundation.Size(25, 50);
     arcSegment.Point = new Windows.Foundation.Point(50, 0);
     arcSegment.RotationAngle = 180;
     pathFigure.IsClosed = true;
     pathFigure.Segments.Add(arcSegment);
     path.Data = pathGeometry;
     path.Stroke = new SolidColorBrush(Colors.Red);
     path.StrokeThickness = 2;
     path.Margin = new Thickness(40, 40, 0, 0);
     pathGeometry.Figures.Add(pathFigure);
     gridroot.Children.Add(path);
 }

结果:

总之,您应该能够找到 API 来绘制您在表单中完成的相同内容。