沒有任何等價物System.Drawing.Drawing2D.GraphicsPath.AddArc,在XAML UWP渲染?
在傳統的Windows窗體應用程序中,我們有用於繪製圖形的System.Drawing.Drawing2D
命名空間。但是這個命名空間在UWP應用程序中不受支持。 UWP應用程序擁有自己的API來繪製圖形。您應該能夠通過Windows.UI.Xaml.Shapes
命名空間下的API繪製形狀。有關如何在uwp應用程序上繪製形狀的更多細節,請參閱this article。
方法System.Drawing.Drawing2D.GraphicsPath.AddArc
實際上是繪製一個弧,你已經在uwp應用程序中找到了,你可以使用添加ArcSegment
來代替。
但上面不等同於System.Drawing.Drawing2D.GraphicsPath.AddArc(矩形,單人間,單人間)100%
UWP應用程序有其自己的API繪製,它可能沒有一種方法,它具有與Windows窗體中相同的方法名稱和參數。但你應該可以在uwp中使用其他方法來實現相同的效果。例如,AddArc
方法有三個參數,Rectangle
用於定義x-直徑和y-直徑以及形狀位置,以及startAngle
和sweepAngle
用於定義開始和結束角度。在uwp應用程序中,ArcSegment
可以通過size
屬性定義圓弧x半徑和y半徑,並指定定義開始和結束位置。
例如,在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來繪製您在表單中完成的相同操作。
感謝您的回答。 –