2016-12-27 13 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 : TextCellTextCellCellから継承し、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で正常にテストされました。 私はまた、元のTodoサンプルには、一度にコメントアウトされた公開インジケータのハッキングがあるので、解決策hereとして提案しました。

関連する問題