将 Xamarin.Forms.Shapes.Path 转换为 MAUI

问题描述 投票:0回答:2

我想在网格内画一个圆。我的旧 Xamarin 代码如下所示:

public int DrawCircle(Brush colour, int strokeThickness, int x, int y, int width, int height, Grid cv)
{
    Path c1 = new Path();
    c1.Stroke = colour;
    c1.Fill = transparentBrush;
    c1.StrokeThickness = strokeThickness;
    EllipseGeometry myEllipseGeometry = new EllipseGeometry();
    myEllipseGeometry.Center = new Point(x, y);
    myEllipseGeometry.RadiusX = height;
    myEllipseGeometry.RadiusY = width;
    c1.Data = myEllipseGeometry;
    cv.Children.Add(c1);

    return cv.Children.Count - 1;
}

我似乎无法弄清楚 MAUI 的做法。

提前致谢

c# xamarin path geometry maui
2个回答
0
投票

将 Xamarin.Forms.Shapes.Path 转换为 MAUI

xamarin 中的

Xamarin.Forms.Shapes.Path
与毛伊岛中的
Microsoft.Maui.Controls.Shapes.Path
相同。

所以你可以添加以下命名空间,然后直接使用你的代码。

using Path = Microsoft.Maui.Controls.Shapes.Path;

0
投票

我不明白迁移后您的代码到底存在什么问题。 如果您只关心以网格为中心的圆,那么您可以直接使用

Ellipse
而不是
EllipseGeometry

下面的示例确保在网格内绘制一个中心圆;直径等于网格宽度和高度之间的最小值。

private int Draw(Brush colour, int strokeThickness, Grid cv)
    {
        Ellipse myEllipse = new()
        {
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center,
            HeightRequest = cv.Height > cv.Width ? cv.Width : cv.Height,
            WidthRequest = cv.Height > cv.Width ? cv.Width : cv.Height,
            Stroke = Colors.Red,
            StrokeThickness = strokeThickness,
        };

        cv.Children.Add(myEllipse);

        return cv.Children.Count - 1;
    }

如果您不想绘制椭圆而不是圆形(如果网格宽度和高度不相等),则替换为:

HeightRequest = cv.Height,
WidthRequest = cv.Width,
© www.soinside.com 2019 - 2024. All rights reserved.