ICanvas.DrawPath / ICanvas.FillPath ArgumentException

Tim W 26 Reputation points
2022-06-26T09:49:08.6+00:00

I am trying draw a filled polygon using IDrawable and PathF. I am getting an ArgumentException on canvas.FillPath or canvas.DrawPath, stating that "A call to CanvasPathBuilder.BeginFigure occurred, when the figure was already begun.". I am not sure what I am doing wrong. Does anybody have an idea? Here is a reproductive source that should draw / fill a triangle (you can reproduce it by copy / pasting this in the MainPage.xaml.cs of a new Maui project):

namespace CanvasPath  
{  
    public partial class MainPage : ContentPage, IDrawable  
    {  
        public MainPage()  
        {  
            this.Content = new GraphicsView { Drawable = this };  
        }  
  
        public void Draw(ICanvas canvas, RectF dirtyRect)  
        {  
            canvas.StrokeColor = Colors.Green;  
            canvas.FillColor = Colors.Red;  
            var topLeft = new PointF(dirtyRect.Left, dirtyRect.Top);  
            var topRight = new PointF(dirtyRect.Right, dirtyRect.Top);  
            var bottomCenter = new PointF(dirtyRect.Center.X, dirtyRect.Bottom);  
  
            canvas.DrawCircle(topLeft, 10);  
            canvas.DrawCircle(topRight, 10);  
            canvas.DrawCircle(bottomCenter, 10);  
  
            var path = new PathF(topLeft);  
            path.MoveTo(bottomCenter);  
            path.MoveTo(topRight);  
            path.Close();  
  
            //both calls do nothing on Android, but throw an ArgumentException on Windows  
            canvas.DrawPath(path);  
            canvas.FillPath(path);  
        }  
  
        private void OnCounterClicked(object sender, EventArgs e)  
        {  
  
        }  
    }  
}  
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,066 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tim W 26 Reputation points
    2022-06-26T12:13:45.51+00:00

    Okay, I'll retract my question, I was buildung the Path wrongly. This is how it should be done:
    var path = new PathF();
    path.MoveTo(topLeft);
    path.LineTo(topRight);
    path.LineTo(bottomCenter);
    path.Close();