无法将PointF[]转为CGPoint[]:CGContext.AddLines示例代码编译失败
CGContext.AddLines Compilation Error in Xamarin Let's break down what's going on here and how to fix it:
The Root Cause
The CGContext.AddLines method explicitly expects a CoreGraphics.CGPoint[] as its parameter, but the official example uses a System.Drawing.PointF[] instead. These are two distinct array types, and C# doesn't allow implicit conversion between them—hence the error:
cannot convert from 'System.Drawing.PointF[]' to 'CoreGraphics.CGPoint[]'
Solution 1: Use CGPoint Directly in the Array
The simplest fix is to replace all instances of PointF with CGPoint in the array you pass to AddLines:
ctxt.AddLines(new CGPoint[] { new CGPoint(30, 30), new CGPoint(60, 30), new CGPoint(40, 40) });
Solution 2: Convert Existing PointF Arrays to CGPoint Arrays
If you already have a collection of PointF objects (e.g., from other parts of your code), you can convert them to CGPoint using the implicit conversion that Xamarin provides between these two types. Here's how to do it with LINQ:
// Your existing PointF array var pointFArray = new PointF[] { new PointF(30, 30), new PointF(60, 30), new PointF(40, 40) }; // Convert to CGPoint[] var cgPointArray = pointFArray.Select(p => (CGPoint)p).ToArray(); // Now pass to AddLines ctxt.AddLines(cgPointArray);
Why Your CGPath Code Works
You might be wondering why your CGPath.AddLines code runs without issues. That's because CGPath.AddLines has an overload that accepts an IEnumerable<CGPoint>, and since PointF can be implicitly converted to CGPoint, when you initialize a CGPoint[] with PointF values, each PointF gets converted automatically. CGContext.AddLines doesn't have this flexible overload—it only accepts a CGPoint[], so you can't pass a PointF[] directly.
内容的提问来源于stack exchange,提问作者Joseph

