Completed
Last Updated: 18 Mar 2019 15:48 by ADMIN
Chris
Created on: 28 Jan 2019 13:53
Category: ListView
Type: Bug Report
5
ListView: [iOS] Using ItemTemplateSelector with DataTemplates with custom cell type raises an exception
An exception on iOS is thrown when the ItemTemplateSelector uses DataTemplate with custom cells
2 comments
ADMIN
Georgi
Posted on: 18 Mar 2019 15:48
Available in minor release 2019.1.318. It will also be available in the R2 2019 release.
Chris
Posted on: 28 Jan 2019 17:04

Here is some more information the issue. You can not use the ItemTempalteSelector the same as the Xamarin DataTemplateSelector. With the Xamarin version you can specify all of the data templates on the template selector, this allows for an easy hand off to different ListViews to select between a wide variety of data templates automatically as how by this example here.

Here is an example of how the ItemTemplateSelector should work to match Xamarin capability:

public class TemplateSelector : DataTemplateSelector

{
        private static DataTemplate _customCellTemplate = new DataTemplate(typeof(CustomCell));
        private static DataTemplate _otherCustomCellTemplate = new DataTemplate(typeof(OtherCustomCell));

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            if (!(item is CustomCellViewModel cvm)) return null;
            if (cvm.ID > 0)
            {
                if (cvm.ID % 2 == 0)
                    return _otherCustomCellTemplate;
                return _customCellTemplate;
            }
            throw new NotImplementedException();
        }
}

 

Here is an example of the provided workaround:

public class TemplateSelector : DataTemplateSelector
{
        public DataTemplate CustomCellTemplate { get; set; }
        public DataTemplate OtherCustomCellTemplate { get; set; }
        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            if (!(item is CustomCellViewModel cvm)) return null;
            if (cvm.ID > 0)
            {
                if (cvm.ID % 2 == 0)
                    return this.OtherCustomCellTemplate;
                return this.CustomCellTemplate;
            }
            throw new NotImplementedException();
        }

}

 

The second half of the workaround is that you have to specify the data template on a per ListView basis, for a smaller app this can be implemented. For a large app this is harder because you can have a large amount of ListViews that either use the same templates or require unique templates. Thus this workaround adds a large amount of code to fix just the iOS platform because Android and UWP work correctly with the Xamarin approach. 

 

I have attached the sample project that I created for the Telerik team to examine the problem. For another example of the workaround you can review the current Telerik Xamarin UI documents located here. I apologize for the format of the code within this comment, it was hard to make it look decent.