将控件添加到自定义单元格时出现异常

时间:2013-06-21 13:14:49

标签: ios xamarin mvvmcross

使用Xamarin和自定义表格单元格(从UITableViewCell派生),这很好用:

[Register("MyCell")]
public class MyCell: UITableViewCell
{
    private UILabel myLabel;

    public MyCell(IntPtr handle)
        : base(handle)
    {
        myLabel= new UILabel()
        {
            Font = UIFont.SystemFontOfSize(16),
            Text = "My Label",
            BackgroundColor =  UIColor.Cyan
        };

        ContentView.Add(myLabel);
    }
}

但是,如果我从MvxTableViewCell派生而来(因为我最终希望我的单元格成为数据绑定),我会在ContentView.Add(myLabel)行上抛出异常:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

这是在任何绑定任何数据的尝试之前。

任何想法是怎么回事?使用Mvx在代码中创建自定义单元格的正确方法是什么?我使用.xib文件让自定义单元格与Mvx一起使用,但在尝试使用PC上的visual studio和Mac上的Xamarin工作室时,它们似乎有点笨拙。

1 个答案:

答案 0 :(得分:1)

如果您的代码与上面完全相同,只有UITableViewCell替换为MvxTableViewCell并且您没有在外部进行任何绑定来更改此内容,那么MvxTableViewCell中没有任何内容可以导致此问题 - 来自{{ 3}} - 它只是UITableViewCell

之上的一个简单图层

在我看来,最好的办法是收集更多基本信息:

  • 是否存在TargetInvocationException的内部异常?如果是这样,抛出内部异常在哪里?
  • 反射本身在哪里发生?

如果你可以解决这个问题,那么你可能有更多机会调试和解决问题

相关问题