2016-12-27 56 views
2

在Xamarin.Forms中使用ViewCellRenderer時,是否有人遇到過下列問題?Xamarin.Forms.Platform.iOS.ViewCellRenderer.GetCell例外:指定的轉換無效

我想通過自定義渲染器向我的Xamarin.Forms自定義ViewCell添加披露指標。

當在自定義渲染器中調用base.GetCell(item, reusableCell, tv)時,它會觸發異常: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; 
     } 
    } 
} 

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

回答

1

看看你的代碼被用於任何PriceControlViewCell是。我的猜測是,這不是因爲this一個ViewCell失敗:

var viewCell = (ViewCell)item; 

UPDATE:

使用PriceControlViewCell : ViewCell而不是PriceControlViewCell : TextCellTextCell繼承自Cell,與ViewCell相同,而不是TextCell : ViewCell

+0

謝謝約翰!我添加了ViewCell實現,並且我認爲我看到了這個問題:我不允許在ViewCellRenderer中使用TextCell嗎? –

1

剛剛有同樣的問題,發現有TextCellRenderer。我所要做的只是更改自定義渲染器的基類。對你來說,這將意味着,而不是

public class PriceControlViewCellCustomRenderer : ViewCellRenderer 

使用本:

public class PriceControlViewCellCustomRenderer : TextCellRenderer 

的代碼的其餘部分可以保持不變。在使用Xamarin 4.5的Visual Studio 15構建的iPhone 5上成功測試。 我也提出這是一個解決方案here,因爲他們的原始Todo樣本曾被推薦爲破解披露指標的黑客。