首页 > 解决方案 > Xamarin iOS 在 NSBundle.MainBundle.LoadNib("CustomView", this, null) 中崩溃

问题描述

我创建了新的 CustomView.xib 文件并与代码隐藏类相关联。但是当我尝试初始化视图时,应用程序崩溃了,如下所述的错误。

MonoTouch:无法安装 sigaction 覆盖,意外的 sigaction 实施。

NSBundle.MainBundle.LoadNib("CustomView", this, null); // 应用程序在此处崩溃。

CS类文件:

public partial class CustomView : UIView
{
    public CustomView(IntPtr handle) : base(handle)
    {

    }

    public override void AwakeFromNib()
    {
        base.AwakeFromNib();
    }
}

设计师类:

[Register("CustomView")]
partial class CustomView
{
    [Outlet]
    UIKit.UIView contentView { get; set; }

    void ReleaseDesignerOutlets ()
    {
        if (contentView != null) {
            contentView.Dispose ();
            contentView = null;
        }
    }
}

笔记:

  1. 构建操作设置为 interfaceDefinition。
  2. xib 的文件所有者设置为类名“CustomView”
  3. 还实现了 AwakeFromNib 覆盖方法。

提前致谢。

编辑:

很长一段时间后,我们遇到了类似的问题,但在 AwakeFromNib 方法中的“contentView”上显示“Object Null Reference”有不同的例外。当您尝试访问 AwakeFromNib 方法中的 ContentView 时,应用程序崩溃。这仅发生在 iOS 9.3.x 上。如果某人面临同样的问题,以下修复可能会对他们有所帮助。

[Export("initWithFrame:")]
public SampleHeaderView(CGRect frame) : base(frame)
{
    Initialize();
}

[Export("initWithCoder:")]
public SampleHeaderView(NSCoder coder) : base(coder)
{
    Initialize();
}

public override void AwakeFromNib()
{
    base.AwakeFromNib();
}

private void Initialize()
{
    if (this.ContentView != null)
    {
        this.ContentView.BackgroundColor = UIColor.Red();
    }
}

标签: xamarin.ios

解决方案


推荐阅读