Has anyone experienced the following problem when using a ViewCellRenderer
in Xamarin.Forms?
I am trying to add a Disclosure Indicator to my Xamarin.Forms custom ViewCell
via a Custom Renderer.
When base.GetCell(item, reusableCell, tv)
gets called in the Custom Renderer, it triggers an Exception: Specified cast is not valid
.
I am using Xamarin.Forms v2.3.3.175.
ViewCell Renderer
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using SampleApp;
using SampleApp.iOS;
[assembly: ExportRenderer(typeof(PriceControlViewCell), typeof(PriceControlViewCellCustomRenderer))]
namespace SampleApp.iOS
{
public class PriceControlViewCellCustomRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
return cell;
}
}
}
ViewCell
using Xamarin.Forms;
namespace SampleApp
{
public class PriceControlViewCell : TextCell
{
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
Text = "";
Detail = "";
var item = BindingContext as PriceControlModel;
Text = "Configuration Id";
Detail = item.ConfigurationId.ToString();
}
}
}
Stack Trace
at Xamarin.Forms.Platform.iOS.ViewCellRenderer.GetCell (Xamarin.Forms.Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.iOS\Cells\ViewCellRenderer.cs:13
at SampleApp.iOS.PriceControlViewCellCustomRenderer.GetCell (Xamarin.Forms.Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv) [0x00005] in /Users/brandonm/Projects/GitHub/SampleApp/iOS/CustomRenderers/PriceControlViewCellCustomRenderer.cs:16
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/3969/8b53676d/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/3969/8b53676d/source/xamarin-macios/src/UIKit/UIApplication.cs:63
at SampleApp.iOS.Application.Main (System.String[] args) [0x00035] in /Users/brandonm/Projects/GitHub/SampleApp/iOS/Main.cs:25
Take a look at your code being used for whatever
PriceControlViewCell
is. My guess is that it's not aViewCell
since this is failing:UPDATE:
Use
PriceControlViewCell : ViewCell
instead ofPriceControlViewCell : TextCell
.TextCell
inherits fromCell
, same asViewCell
and is notTextCell : ViewCell
.