Xamarin.Forms.Platform.iOS.ViewCellRenderer.GetCell 异常:指定的转换无效。

3

有没有人在使用Xamarin.Forms的ViewCellRenderer时遇到以下问题?

我正在尝试通过自定义渲染器在我的Xamarin.Forms自定义ViewCell中添加一个披露指示器。

当在自定义渲染器中调用base.GetCell(item, reusableCell, tv)时,会触发异常:指定的转换无效

我正在使用Xamarin.Forms v2.3.3.175。

ViewCell渲染器

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

调用栈

  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 

1
如果您可以发布 PriceControlViewCell 的源代码并显示它是有效的“ViewCell”,我会更新我的答案。 - therealjohn
2个回答

1

看一下你的代码,用于任何PriceControlViewCell。我猜它不是一个ViewCell,因为this失败了:

var viewCell = (ViewCell)item;

更新:

使用PriceControlViewCell : ViewCell代替PriceControlViewCell : TextCellTextCell继承自Cell,与ViewCell相同,并且不是TextCell : ViewCell


谢谢John!我添加了我的ViewCell实现,我想我发现了问题:我不能在ViewCellRenderer中使用TextCell吗? - Brandon Minnick

1

我刚遇到了相同的问题,并发现有一个名为TextCellRenderer的类。我所需要做的就是更改自定义渲染器的基类。在您的情况下,这意味着您需要将其更改为:

public class PriceControlViewCellCustomRenderer : ViewCellRenderer

使用此代码:
public class PriceControlViewCellCustomRenderer : TextCellRenderer

其余代码可以保持不变。在使用Visual Studio 15和Xamarin 4.5构建的iPhone 5上成功测试。 我还提出了这里的解决方案,因为他们原始的Todo示例已经注释掉了一次推荐的披露指标hack。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接