Bug 45092 - Group property of ItemTappedEventArgs can't be cast to original type, TemplatedItemList, since it is internal.
Summary: Group property of ItemTappedEventArgs can't be cast to original type, Templat...
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.3.2
Hardware: Macintosh Mac OS
: --- enhancement
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-10-06 01:33 UTC by Jon Goldberger [MSFT]
Modified: 2017-03-22 17:29 UTC (History)
6 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jon Goldberger [MSFT] 2016-10-06 01:33:53 UTC
## Description

In the ItemTapped event handler for a grouped ListView, the ItemTappedEventArgs has two properties, Item and Group. Whereas Item gives you the view model fro the cell, as would be expected, the Group property contains type TemplatedItemsList, which is an internal class in Xamarin,Forms.Core. As such one can not cast to this type to access its public properties, like Name, etc. 

>void LstView_ItemTapped (object sender, ItemTappedEventArgs e)
>     var group = e.Group as Xamarin.Forms.TemplatedItemsList<Xamarin.Forms.ItemsView<Xamarin.Forms.Cell>, Xamarin.Forms.Cell>;

does not work since Xamarin.Forms.TemplatedItemsList is internal. 

It seems to me if you are going to pass a property in an event argument it should be usable without having to use Reflection or other tricks to cast the property to the type needed to access the public fields for the type. 

## Steps to reproduce.

1. Get the Xamarin.Forms ListView Sample: Grouping solution from:

2. Open the App.cs file and change:
> MainPage = new NavigationPage(new GroupedListXaml ());
>MainPage = new NavigationPage(new GroupedListCode ());

3. Open GroupedListCode.cs file.

4. In the constructor, subscribe to the ListView's ItemTapped event:
> lstView.ItemTapped += LstView_ItemTapped;

5. Implement the event handler:
> void LstView_ItemTapped (object sender, ItemTappedEventArgs e)
>    var group = e.Group as Xamarin.Forms.TemplatedItemsList<Xamarin.Forms.ItemsView<Xamarin.Forms.Cell>, Xamarin.Forms.Cell>;

Expected result: Should be able to cast to the type that is passed in e.Group. 

Actual result: Cannot cast to the passed in type due to its protection level (internal)

## Notes

Tested with XF version 2.3.2 and 2.3.3-pre2 with the same results in each. It would seem best to make the TemplatedItemsList class public and just set the fields that you don't want other assemblies to mess with as internal. 

## Environment

=== Xamarin Studio Enterprise ===

Version 6.1.1 (build 15)
Installation UUID: ceaba76c-db06-4fbd-b326-f69ea53c3e01
	Mono 4.6.1 (mono-4.6.0-branch-c8sr0/ef43c15) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406010005

=== NuGet ===


=== Xamarin.Profiler ===

Version: 0.33.1
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 8.0 (11246)
Build 8A218a

=== Xamarin.Android ===

Version: (Visual Studio Enterprise)
Android SDK: /Users/jongoldberger/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)
		7.0   (API level 24)

SDK Tools Version: 25.2.2
SDK Platform Tools Version: 24.0.3
SDK Build Tools Version: 24.0.2

Java SDK: /usr
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

Android Designer EPL code available here:

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: (Visual Studio Enterprise)
Hash: ad1cd42
Branch: cycle8-sr0-xi
Build date: 2016-10-03 15:18:44-0400

=== Xamarin.Mac ===

Version: (Visual Studio Enterprise)

=== Build Information ===

Release ID: 601010015
Git revision: fa52f02641726146e2589ed86ec4097fbe101888
Build date: 2016-09-22 08:03:02-04
Xamarin addins: 75d65712af93d54dc39ae4c42b21dfa574859fd6
Build lane: monodevelop-lion-cycle8-sr0

=== Operating System ===

Mac OS X 10.11.6
Darwin Jons-MacBook-Pro.local 15.6.0 Darwin Kernel Version 15.6.0
    Mon Aug 29 20:21:34 PDT 2016
    root:xnu-3248.60.11~1/RELEASE_X86_64 x86_64
Comment 2 adrianknight89 2016-10-15 18:44:55 UTC
See https://github.com/xamarin/Xamarin.Forms/pull/461
Comment 3 Rui Marinho 2017-01-03 11:39:33 UTC
Should be fixed in 2.3.4-pre2
Comment 4 Parmendra Kumar 2017-02-06 11:55:08 UTC
I have checked this issue with latest and I am still getting same issue.

Screencast: https://www.screencast.com/t/sjkDR06rQN

Error:  Error CS0122: `Xamarin.Forms.TemplatedItemsList<TView,TItem>' is inaccessible due to its protection level (CS0122) (groupingSampleListView)

Hence reopened this issue.

Comment 5 adrianknight89 2017-02-06 12:11:36 UTC

I don't believe you're testing it the right way. TemplatedItemsList should still be internal, but you should be able to convert e.Group to your model.

void OnItemTapped(Object sender, ItemTappedEventArgs e)
    var item = (MyItem)(e.Item);
    var group = (MyGroup)(e.Group);
Comment 6 Samantha Houts [MSFT] 2017-03-22 17:29:11 UTC
Comment 5 is correct. Instead of exposing an internal class, the ItemTappedEventArgs now contains the actual model passed in. The sample referenced in the original report will need to be updated, at which point it should work.

Thank you!