Hello, I'm running into a weird issue:
I'm using a slide view with an item template selector to select the data template for each item in the items source.
For some reason, the OnSelectTemplate method in the template selector will never be called more than three times ever. The first call is made with the last item in the items source, the second call with the first item and the third call with the second item.
My items source collection has at four items, so the third template is never selected...
Here's the XAML:
<
telerikPrimitives:RadSlideView
HorizontalContentOptions
=
"CenterAndExpand"
VerticalContentOptions
=
"CenterAndExpand"
HorizontalOptions
=
"FillAndExpand"
VerticalOptions
=
"FillAndExpand"
>
<
telerikPrimitives:RadSlideView.ItemsSource
>
<
x:Array
Type
=
"{x:Type x:Int32}"
>
<
x:Int32
>1</
x:Int32
>
<
x:Int32
>2</
x:Int32
>
<
x:Int32
>3</
x:Int32
>
<
x:Int32
>4</
x:Int32
>
<
x:Int32
>5</
x:Int32
>
<
x:Int32
>6</
x:Int32
>
<
x:Int32
>7</
x:Int32
>
<
x:Int32
>8</
x:Int32
>
</
x:Array
>
</
telerikPrimitives:RadSlideView.ItemsSource
>
<
telerikPrimitives:RadSlideView.ItemTemplateSelector
>
<
local:TemplateSelector
>
<
local:TemplateSelector.Template1
>
<
DataTemplate
>
<
ContentView
>
<
Label
Text
=
"Bug"
HorizontalTextAlignment
=
"Center"
VerticalTextAlignment
=
"Center"
/>
</
ContentView
>
</
DataTemplate
>
</
local:TemplateSelector.Template1
>
</
local:TemplateSelector
>
</
telerikPrimitives:RadSlideView.ItemTemplateSelector
>
</
telerikPrimitives:RadSlideView
>
And here's the template selector class:
public class TemplateSelector : DataTemplateSelector
{
public DataTemplate Template1 { get; set; }
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
return Template1;
}
}
Put a breakpoint in the return statement and count the number of times the method gets called, while also watching the item.
Am I doing this properly? Is this a bug in the way the items source is being iterated over?
9 Answers, 1 is accepted
Rereading my original post I realize the example code could be misinterpreted.I understand that I would need eight different item templates in XAML and to return them appropriately from the template selector. I chose to keep the code shorter though.
My point is really about the number of calls made to OnSelectTemplate. I expect eight calls and I see only three.
You're on the right path, but you've only got one DataTemplate defined. You return the different templates inside OnSelectTemplate
public
class
TemplateSelector : DataTemplateSelector
{
public
DataTemplate FallBackTemplate {
get
;
set
; }
public
DataTemplate Template1 {
get
;
set
; }
public
DataTemplate Template2 {
get
;
set
; }
public
DataTemplate Template3 {
get
;
set
; }
protected
override
DataTemplate OnSelectTemplate(
object
item, BindableObject container)
{
if
(item
is
int
value)
{
switch
(value)
{
case
1:
return
Template1;
case
2:
return
Template2;
case
3:
return
Template3;
}
}
return
FallBackTemplate;
}
}
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Thank you Lance, as I mentioned in my follow up post, I understand I need to return different templates - I should've make that clear in my original post. The problem is that OnSelectTemplate does not appear to be called more than 3 times ever, regardless of how many items are in the ItemsSource collection.
My apologies! I'll test this now. So that I can make sure I'm using the right environment, can you reply with the following info:
What version of Xamarin.Forms are You using?
What platform are you targeting?
-- Is this this Android, iOS or UWP?
-- What version of the OS/SDK?
In the meantime, I'll spin up the project using your initial code example using the defaults.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Thanks. Here are the details you requested:
Telerik_UI_for_Xamarin_2018_2_516_2_Dev
Shared code project targets .NET Standard 1.4
Xamarin Forms 2.5.1.527436
Android and iOS
Android 8.1 (API Level 27 - Oreo) - on emulator
iOS 11 - on simulator
Did not yet try on real devices.
I have been able to reproduce the issue using your initial code example and occurs on all platforms. It appears to reuse the template for several items in a row and then makes another call again for subsequent items.
I have logged this with the development team, you can follow the bug report here: SlideView: ItemTemplateSelector Not Working Correctly. I have also updated your Telerik points as a small token of appreciation for bringing this to our attention.
Workaround
Unfortunately, I am not able to come up with a workaround for the selector. An alternative would be to use a single ItemTemplate and ValueConverters to change the content's properties.
For example:
public
class
Item
{
public
string
DisplayValue {
get
;
set
; }
public
int
Value {
get
;
set
; }
}
public
class
MyContentConverter : IValueConverter
{
public
object
Convert(
object
value, Type targetType,
object
parameter, CultureInfo culture)
{
return
(
int
)value % 2 == 0 ? Color.Blue : Color.Red;
}
public
object
ConvertBack(
object
value, Type targetType,
object
parameter, CultureInfo culture)
{
throw
new
NotImplementedException();
}
}
<
ResourceDictionary
>
<
portable:MyContentConverter
x:Key
=
"MyContentConverter"
/>
</
ResourceDictionary
>
...
<
telerikPrimitives:RadSlideView.ItemTemplate
>
<
DataTemplate
>
<
ContentView
>
<
StackLayout
VerticalOptions
=
"Center"
HorizontalOptions
=
"Center"
>
<
Label
Text
=
"{Binding DisplayValue}"
TextColor
=
"{Binding Value, Converter={StaticResource MyContentConverter}}"
/>
</
StackLayout
>
</
ContentView
>
</
DataTemplate
>
</
telerikPrimitives:RadSlideView.ItemTemplate
>
This would result in a different color being used for every other item.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
I have updated your points for the report. In addition, I'd like to inform a fix will be available in the upcoming minor release expected this week.
Regards,
Yana
Progress Telerik