2

I am in the process of making a custom horizontally scrollable ListView type control, I have come across an issue which I can't explain, and I'm not 100% sure that the issue is even with my code, unless I have misunderstood something.

I have distilled the issue into the simplest form I can, available at https://github.com/sparkeh9/XamarinFormsListViewIssue.

So, I have a HorizontalListView control utilising a very simple data template selector (no logic, returns a single data template)

See XAML screenshot

See Template Selector screenshot

My issue is, when I use a template selector, when calling ItemTemplateSelector.CreateContent() in order to start generating UI controls based on the template, it throws an exception See exception screenshot which claims that the LoadTemplate property should not be null. I have looked at the base classes and found that this property is a Func<object> which is private, and can only be set from outside by calling a specific constructor.

If I specify a literal item template in the XAML, everything works as expected, such as:

<controls:HorizontalListView.ItemTemplate>
    <DataTemplate>
        <Label Text="test"></Label>
    </DataTemplate>
</controls:HorizontalListView.ItemTemplate>

EDIT: Answered my own question

sparkeh9
  • 75
  • 1
  • 10
  • It might be nonsense, but if you specify both an `ItemTemplateSelector` and an `ItemTemplate`, wouldn't one override the other? Maybe it's what causing the issue? Nevertheless, please post the entire `HorizontalListView.Style` – TomerAgmon1 Apr 15 '18 at 08:13
  • I added ItemTemplate and ItemTemplateSelector after experiencing the issue, it was just to prove that it was the selector causing the issue :) – sparkeh9 Apr 15 '18 at 09:55
  • Also, there is no style defined – sparkeh9 Apr 15 '18 at 14:06

1 Answers1

5

I figured out what I had done wrong - if the type is DataTemplateSelector, then you must first generate a template by calling ItemTemplateSelector.SelectTemplate( item, null ), after which point you can call CreateContent()

diff

I've committed this to demonstrate what I mean: https://github.com/sparkeh9/XamarinFormsListViewIssue/commit/f2f0d807d2d463d1fe9e6a89f0d3c0c7676a0761

sparkeh9
  • 75
  • 1
  • 10