UICollectionView F# 初始化中的调用方法

UICollectionView F# Call method in init

 [<Register ("ChatViewCell")>]
type ChatViewCell (handle: IntPtr) as this = 
    inherit UICollectionViewCell (handle)


    [<DefaultValue>] static val mutable private id : NSString

    static member init = 
        printfn "Initializing ChatViewCell."
        ChatViewCell.id <- new NSString("ChatCell")


    override this.ReuseIdentifier = ChatViewCell.id

    let mutable profileImageView = new UIImageView()
    let mutable nameLabel = new UILabel()
    let mutable messageLabel = new UILabel()
    let mutable timeofMessageLabel = new UILabel()
    let mutable dividerLineView = new UIView()
    let mutable countLabel = new UILabel()

    let setupView() = 
        profileImageView.Frame <- CGRect(50.0, 0.0, 200.0, 100.0)
        profileImageView.ContentMode <- UIViewContentMode.ScaleAspectFill
        profileImageView.Layer.CornerRadius <- Conversions.nfloat(30)

我有以下 UICollectionViewCell 并且我想在初始化单元格时调用 setupView 方法。但是,setupView 方法似乎在 init 中不可用。我尝试将其移动到 init 以上,但是,这也不起作用。

setupView 被定义为实例函数,因为它没有 static 修饰符。它必须是实例函数(或实例方法),因为它访问 profileImageView 这是一个实例字段。

静态成员init无法调用实例函数,因为无法将实例显式传递给实例函数(只能显式将实例传递给方法)。

如果您想对 ChatViewCell 的构造进行一些初始化,您只需将初始化语句放在 class 的主体中即可。执行此操作时,您需要使用通常隐式的 do 关键字。

例如

type ChatViewCell (handle: IntPtr) as this = 
    inherit UICollectionViewCell (handle)

    let mutable profileImageView = new UIImageView()

    do profileImageView.Frame <- CGRect(50.0, 0.0, 200.0, 100.0)
    do profileImageView.ContentMode <- UIViewContentMode.ScaleAspectFill
    do profileImageView.Layer.CornerRadius <- Conversions.nfloat(30)

有用的参考资料: