You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法将PointF[]转为CGPoint[]:CGContext.AddLines示例代码编译失败

Fixing the 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:04:52