简体   繁体   中英

NullReferenceException when using custom UITableViewCell with ViewCellRenderer

i am trying to setup a test project, where i use platform specific implementation for a ViewCell within a Xamarin.Forms ListView. In UWP everything works as expected. But when i try to open that page on IOS Simulator. I retrieve NullReferenceException without any additional hints at:

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

which of course does not help very much.

The Forms XAML Looks like that:

<?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>

and also the native cell implementation is very overseeable:

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);
    }
}

It's used within the 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;
        }
    }
}

None of my breakpoints has been hit. So i really don't know where this exception is coming from. Can someonge give me a hint? My Code is based on the tutorial from this site: https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/viewcell/ May the Information be to old??

I am using Xamarin.Forms 2.3.3.180 within Visual Studio 2017RC.

cheers, Chris

UPDATE:

"Funny" behavior: When i surround the Main call with an try/catch, i retrieve more Information about the exception. Here, is what the exception stack says:

  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 

Solution:

I found the problem. I used a class library for my control. During compilation the reference to that class library was removed by the compiler, cause it did not recognize any usage to that library. So i have written a static dummy class with one dummy method, which is now called out of the main iOS Application. Now he is able to find the customer renderer.

I found the problem. I used a class library for my control. During compilation the reference to that class library was removed by the compiler, cause it did not recognize any usage to that library. So i have written a static dummy class with one dummy method, which is now called out of the main iOS Application. Now he is able to find the customer renderer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM