在不添加新构造函数的情况下向下转换类

本文关键字:转换 情况下 添加 构造函数 | 更新日期: 2023-09-27 18:36:14

美好的一天,

我正在尝试为 (目标C) 创建一个绑定

@interface GPUImageView : UIView <GPUImageInput>
{
    GPUImageRotationMode inputRotation;
}

通过在 ApiDefinitions.cs (MonoTouch) 中编写此内容

[BaseType (typeof(UIView))]
interface GPUImageView : GPUImageInput{
    [Export ("initWithFrame:")]
    IntPtr Constructor(RectangleF frame);
    [Export ("autoresizingMask")]
    UIViewAutoresizing AutoResizingMask { get; set;}
    [Export ("addSubView:")]
    void AddSubView(UIView view);
}

当我打电话时,我得到一个"无效的投射异常"

GPUImage.GPUImageView filterView = (GPUImage.GPUImageView)this.View;

我可以创建一个使用反射的构造函数。但这真的是正确的方法吗?

干杯!

在不添加新构造函数的情况下向下转换类

正如 Rolf Bjarne Kvinge 指出的那样,解决方案是将 Interface Builder 中的类类型更改为 GPUImageView。这将创建一个应该从 GPImageView 继承的类,而 GPImageView 又从 UIView 继承。也不需要绑定 AddSubview。

相关文章: