与ViewCellRenderer一起使用自定义UITableViewCell时出现NullReferenceException

时间:2017-01-17 16:56:38

标签: c# ios uitableview xamarin.ios xamarin.forms

我正在尝试设置一个测试项目,我在Xamarin.Forms ListView中使用ViewCell的平台特定实现。在UWP中,一切都按预期工作。但是,当我尝试在IOS模拟器上打开该页面时。我检索NullReferenceException而没有任何其他提示:

UIApplication.Main(args, null, "AppDelegate");

这当然没有多大帮助。

表格XAML看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             xmlns:controls="clr-namespace:My.Controls;assembly=My.Controls"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="PrismFormsPrototype.Views.TestPage">
    <StackLayout HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand">
        <ListView x:Name="PlatformSpecificCellsList"  
                  CachingStrategy="RecycleElement" 
                  BindingContext="{Binding MyCompanies}" 
                  ItemsSource="{Binding Companies}" BackgroundColor="Gray">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <controls:TileCell Text="{Binding Text}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

并且本机单元实现也非常可操作:

internal class NativeTileCell : UITableViewCell, INativeElementView
{
    public UILabel HeadingLabel { get; set; }
    public TileCell TileCell { get; private set; }
    public Element Element => TileCell;

    public NativeTileCell(string cellId, TileCell cell) : base(UITableViewCellStyle.Default, cellId)
    {
        TileCell = cell;

        SelectionStyle = UITableViewCellSelectionStyle.Gray;
        ContentView.BackgroundColor = UIColor.FromRGB(255, 255, 224);

        HeadingLabel = new UILabel()
        {
            Font = UIFont.FromName("Cochin-BoldItalic", 22f),
            TextColor = UIColor.FromRGB(127, 51, 0),
            BackgroundColor = UIColor.Clear
        };

        ContentView.Add(HeadingLabel);
    }

    public void UpdateCell(TileCell cell)
    {
        HeadingLabel.Text = cell.Text;
    }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        HeadingLabel.Frame = new CGRect(5, 4, ContentView.Bounds.Width - 63, 25);
    }
}

在NativeTileCellRenderer中使用。

[assembly: ExportRenderer(typeof(TileCell), typeof(NativeTileCellRenderer))]
namespace My.Controls.iOS
{
    public class NativeTileCellRenderer : ViewCellRenderer
    {
        NativeTileCell cell;

        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var tileCell = item as TileCell;
            if (tileCell == null)
                return null;

            cell = reusableCell as NativeTileCell;
            if (cell == null)
                cell = new NativeTileCell(item.GetType().FullName, tileCell);
            else
                cell.TileCell.PropertyChanged -= OnNativeCellPropertyChanged;

            tileCell.PropertyChanged += OnNativeCellPropertyChanged;
            cell.UpdateCell(tileCell);
            return cell;
        }

        private void OnNativeCellPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var tileCell = sender as TileCell;
            if (tileCell == null)
                return;

            if (e.PropertyName == TileCell.TextProperty.PropertyName)
                cell.HeadingLabel.Text = tileCell.Text;
        }
    }
}

我的断点都没有被击中。所以我真的不知道这个例外的来源。有些人可以给我一个提示吗?我的代码基于本网站的教程:https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/viewcell/可能信息是旧的吗?

我在Visual Studio 2017RC中使用Xamarin.Forms 2.3.3.180。

欢呼声, 克里斯

更新:

&#34;滑稽&#34;行为:当我用try / catch包围Main调用时,我会检索有关异常的更多信息。这里是异常堆栈的内容:

  at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Platform.cs:281 
  at Xamarin.Forms.Platform.iOS.ViewCellRenderer+ViewTableCell.GetNewRenderer () [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Cells\ViewCellRenderer.cs:136 
  at Xamarin.Forms.Platform.iOS.ViewCellRenderer+ViewTableCell.UpdateCell (Xamarin.Forms.ViewCell cell) [0x0004e] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Cells\ViewCellRenderer.cs:155 
  at Xamarin.Forms.Platform.iOS.ViewCellRenderer+ViewTableCell.set_ViewCell (Xamarin.Forms.ViewCell value) [0x0000a] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Cells\ViewCellRenderer.cs:65 
  at Xamarin.Forms.Platform.iOS.ViewCellRenderer.GetCell (Xamarin.Forms.Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv) [0x0004d] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Cells\ViewCellRenderer.cs:22 
  at Xamarin.Forms.Platform.iOS.CellTableViewCell.GetNativeCell (UIKit.UITableView tableView, Xamarin.Forms.Cell cell, System.Boolean recycleCells, System.String templateId) [0x00086] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Cells\CellTableViewCell.cs:74 
  at Xamarin.Forms.Platform.iOS.ListViewRenderer+ListViewDataSource.GetCell (UIKit.UITableView tableView, Foundation.NSIndexPath indexPath) [0x00060] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Renderers\ListViewRenderer.cs:727 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3985/a7f1dc3d/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3985/a7f1dc3d/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
  at PrismFormsPrototype.iOS.Application.Main (System.String[] args) [0x00002] in C:\Workspace\PrismFormsPrototype\PrismFormsPrototype\PrismFormsPrototype.iOS\Main.cs:19 

解决方案:

我发现了问题。我使用了一个类库来控制。在编译期间,编译器删除了对该类库的引用,因为它无法识别该库的任何用法。所以我用一个虚拟方法编写了一个静态虚拟类,现在从主iOS应用程序中调出。现在他能够找到客户渲染器。

1 个答案:

答案 0 :(得分:0)

我发现了问题。我使用了一个类库来控制。在编译期间,编译器删除了对该类库的引用,因为它无法识别该库的任何用法。所以我用一个虚拟方法编写了一个静态虚拟类,现在从主iOS应用程序中调出。现在他能够找到客户渲染器。