R. J. R. J. - 3 months ago 19
C# Question

How is INativeElementView supposed to be implemented?

I was trying to implement

ListViewCachingStrategy=RecycleElement
in a Xamarin.Forms xaml page and everything displays correctly until I try to scroll. Then I get an exception that says that INativeElementView needs to be implemented. I designed the views in each native platform's ui designer and have a custom renderer for each. This crash does not happen if I use
RetainElement
instead of
RecycleElement
. Any ideas or suggestions?

Edit: My view that I created in the designer is called
FeeViewCell
and if I understand it right that is where INativeElementView is supposed to be implemented (in FeeViewCell.cs). My problem is now that casting to an element returns a System.InvalidCastException saying that the specified cast is not valid. Here is my code implementing
INativeElementView
:

public Element Element
{
get
{
return this.ToView() as Element;
}
}

I also have this as a question on the Xamarin forum here.

Answer

The INativeNativeView interface needs to be implemented on the custom cell class. In my example in iOS I in the custom renderer provides an item that comes from the pcl in my case a FeeCell. This is part of my custom renderer

public class FeeCellRenderer : ViewCellRenderer
    {
        public override UIKit.UITableViewCell GetCell(Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
        {
            var x = (FeeCell)item;
            //Do whatever you need to here.
         }
      }

This is part of my custom ui class:

public partial class FeeViewCell : UITableViewCell, INativeElementView
{
    public static readonly NSString Key = new NSString(nameof(FeeViewCell));
    Element _element;
public Element Element
        {
            get { return _element; }
            set { _element = value; }
        }
}

It also works to do this:

public Element Element
        {
            get { return new FeeCell(); }
        }