【问题标题】:How to convert RectangleF[] into NSArray of CGRects?如何将 RectangleF[] 转换为 CGRects 的 NSArray?
【发布时间】:2012-12-04 11:50:52
【问题描述】:

我确定我以前做过,但我不知道怎么做:我这里有一个绑定,需要 NSArray。内容为CGRect对象。

如何从RectangleF[] 的数组中获取CGRect 中的NSArray

我必须转换的另一件事是:PointF[][] 转换为 `NSArray of CGPoint[]

请注意,我实际上在这里遇到了绑定问题:

/// Coordinates for highlight annotation (boxed CGRect's)
@property (nonatomic, strong) NSArray *rects;

/// Array of lines (which is a array of CGPoint's)
@property (nonatomic, copy) NSArray *lines;

这是他们目前的样子:

[Export ("rects")]
NSArray Rects { get; set; }

[Export ("lines")]
NSArray Lines { get; set; }

这是无法编译的(在绑定项目中):

[Export ("rects")]
RectangleF[] Rects { get; set; }

[Export ("lines")]
PointF[][] Lines { get; set; }

编辑/解决方案:

按照 PouPou 的建议,我想出了以下工作代码。 要将 PointF 数组的数组转换为 NSArray,我实现了此方法。请注意,它的输入实际上是一个 AnnotationPoint,但它会被转换为 PointF:

/// <summary>
/// Extends AnnotationPoint[][] to allow conversion into an NSArray containing arrays of CGPoint.
/// </summary>
/// <returns>the NSArray</returns>
/// <param name='aStrokePath'>the path to convert</param>
public static NSArray ToPSPDFInkLines(this AnnotationPoint[][] aStrokePath)
{
    List<NSArray> aLines = new List<NSArray>();
    foreach ( AnnotationPoint[] aLine in aStrokePath )
    {
        List<NSObject> aLinePoints = new List<NSObject>();
        foreach (AnnotationPoint oPoint in aLine)
        {
            aLinePoints.Add(NSObject.FromObject(oPoint.ToPointF()));
        }
        var oNSLineArray = NSArray.FromNSObjects(aLinePoints.ToArray());
        aLines.Add(oNSLineArray);
    }
    NSArray oNSArray = NSArray.FromNSObjects(aLines.ToArray());
    return oNSArray;
}

要将 RectangleF[] 转换为 NSArray,这就是我所追求的。请注意,在这里,我使用的是 AnnotationRegion 对象,但它们被转换为 RectangleF:

NSObject[] aRects = oHighlightAnnot.Coords.Select(oRegion => NSObject.FromObject(oRegion.ToRectangleF())).ToArray();
NSArray oNSArray = NSArray.FromNSObjects(aRects);

【问题讨论】:

    标签: c# xamarin.ios core-graphics


    【解决方案1】:

    CoreGraphic 的 typeCGRect 在 MonoTouch 中映射到 System.Drawing 的 RectangleF。在编写绑定时,当您看到前者(在 ObjectiveC 中)时,您会编写后者(在 C# 中)。

    EDIT 但是,绑定生成器无法将NSArray 转换为非NSObject 的数组(例如RectangleF 等值类型)。在这种情况下,您可以将它们绑定为NSArray 并用[Internal] 标记它们,然后为公共消费提供更好的(手动)重载。

    然后你可以创建自己的Rectangle[],根据NSArray的大小,然后迭代NSArray元素得到元素。您需要将每个元素转换(参见NSObjectNSValue 助手)为RectangleF

    当你不知道你会得到什么类型时,你需要保留NSArray

    【讨论】:

    • 嗯...这个答案与 Rolf 在这里写的相反。现在我很困惑和卡住...forums.xamarin.com/discussion/comment/1633#Comment_1633 - 我会在这里更新我的问题。
    • 能否请您说明我将如何将我的 RectangleF[] 和 PointF[][] 设置到相应的 NSArray 中以将它们与绑定一起使用?
    • 我会保留 NSArray,但是当使用 NSArray.FromObjects() 填充(!) NSArray 时,我得到: System.ArgumentException: 不知道如何编组类型为 'System.Drawing 的对象.RectangleF[]' 到 /Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSArray.cs:69 中 MonoTouch.Foundation.NSArray.FromObjects (System.Object[] items) [0x0002c] 的 NSObject 跨度>
    • NSArray.FromObjects 是创建一个NSArray(从上面反向)并调用支持RectangleFNSObject.FromObject。 OTOH,您的原始 NSArray 包含不是 RectangleFNSObject 实例 - 至少在转换之前没有。没有看到实际输入就很难说。
    • 这正是我想要的:从 RectangleF[] 创建一个 NSArray,然后将该 NSArray 分配给绑定方法,这会引发异常。如果我手动将每个 RectangleF 包装到一个 NSObject 中,然后使用 FromNSObjects() 它就可以工作。错误?
    猜你喜欢
    • 2010-11-23
    • 2012-03-13
    • 1970-01-01
    • 2016-07-05
    • 2011-06-01
    • 2010-12-18
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多