3

ViewCellRenderer在 Xamarin.Forms 中使用 a 时是否有人遇到过以下问题?

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

base.GetCell(item, reusableCell, tv)自定义渲染器中调用时,它会触发 Exception: Specified cast is not valid

我正在使用 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;
        }
    }
}

视图单元

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 
4

2 回答 2

1

看看你的代码被用于什么PriceControlViewCell。我的猜测是它不是,ViewCell因为失败了:

var viewCell = (ViewCell)item;

更新:

使用PriceControlViewCell : ViewCell而不是PriceControlViewCell : TextCell. TextCell继承自Cell, 相同ViewCell且不是TextCell : ViewCell

于 2016-12-27T19:18:42.893 回答
1

Just had the same problem, and found that there is TextCellRenderer. All I had to do was change the base class of the custom renderer. In your case, this would mean instead of

public class PriceControlViewCellCustomRenderer : ViewCellRenderer

use this:

public class PriceControlViewCellCustomRenderer : TextCellRenderer

The rest of the code can remain unchanged. Tested successfully on an iPhone 5 built with Visual Studio 15 with Xamarin 4.5. I also proposed this as a solution here, because their original Todo sample has the once recommended hack for the disclosure indicator commented out.

于 2017-06-01T20:06:05.037 回答