This is a weird one, and it's got me stumped.
My XAML follows the following pattern:
<ContentView ... >
<ContentView.Resources>
<ResourceDictionary>
<local:ItemColorConverter x:Key="ItemColorConverter" />
</ResourceDictionary>
</ContentView.Resources>
<ContentView.Content>
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid BackgroundColor="{Binding ColorConverterProperty, Converter={StaticResource ItemColorConverter}, Mode=OneWay}">
(RowDefinitions, ColumnDefinitions, and display elements go here)
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView.Content>
</ContentView>
In my code, I have the following:
public class Item : INotifyPropertyChanged, IComparable<Item>
{
(fields go here)
[JsonIgnore]
public ColorParams ColorConverterProperty
{
get
{
return new ColorParams(things that the IValueConverter needs to access);
}
}
}
My IValueConverter is as follows:
class ItemColorConverter : IValueConverter
{
private static Color ms_cColor1 = Color.FromRgb(255, 255, 255);
private static Color ms_cColor2 = Color.FromRgb(192, 16, 0);
private static Color ms_cColor3 = Color.FromRgb(0, 16, 192);
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ColorParams cpParams;
if (value != null && value.GetType() == typeof(ColorParams))
{
cpParams = (ColorParams)value;
if (cpParams.Value1)
{
return ms_cColor1;
}
else
{
if (cpParams.Value2)
{
return ms_cColor2;
}
else
{
return ms_cColor3;
}
}
}
else
{
return Color.Transparent;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
This all works great. I can fire the INotifyPropertyChanged for ColorConverterProperty event on the data class, and the ListView row background color changes appropriately. This is what it's supposed to do, and this works on Android and UWP.
However, just recently, it's stopped working on iOS devices. Well, kind of stopped working. The event fires, the ListView line's background color changes, but a fraction of a second later, it changes back to the first color. I've traced out my app, and it doesn't fire the event a second time, and the color converter doesn't get called twice, only once. But the background color still changes back.
The weird thing is, if I scroll the item in question off the screen, then scroll it back on the screen, it has the correct background color again. It's almost like iOS redraws the item on the screen with the old background color, and only redraws the item when it's scrolled back on screen.