I have a shared Xamarin.Forms project, and the problem only exists on Android. My problem is, that I have a listview, and when I click a button in my customcell, it changes color(from blue to green). Then I click another button which opens another page, and when I close that page, the item is removed from the listview. But now the item below the removed one, has a green button, instead of a blue. Here's an example:
The RouteElement Model.
public class RouteElement : INotifyPropertyChanged
{
string arrivalBtnColor;
public event PropertyChangedEventHandler PropertyChanged;
public DateTime ArrivalTime { get; set; }
public DateTime DepartureTime { get; set; }
public bool ReadyForService { get; set; }
public bool DeliveredToService { get; set; }
public string ArrivalBtnBColor
{
get { return arrivalBtnColor; }
set
{
if (arrivalBtnColor != value)
{
arrivalBtnColor = value;
OnPropertyChanged("ArrivalBtnBColor");
}
}
}
public RouteElement()
{
this.ArrivalBtnBColor = "Default";
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
The CustomCell
Button ArrivalBtn = new Button
{
Text = "Ankomst",
FontSize = 24,
BorderRadius = 10,
HeightRequest = 75,
TextColor = Color.FromHex("#FFFFFF")
};
ArrivalBtn.SetBinding(Button.BackgroundColorProperty, "ArrivalBtnBColor",BindingMode.Default, new StringToColorConverter(), null);
Label PostalNoLbl = new Label()
{
TextColor = Color.Black,
HorizontalTextAlignment = TextAlignment.Center,
VerticalOptions = LayoutOptions.Start,
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
};
PostalNoLbl.SetBinding(Label.TextProperty, "Postcode");
PostalNoLbl.SetBinding(Label.IsVisibleProperty, "Postcode", BindingMode.Default,new StringToBoolConverter(),null);
Then I call this MessagingCenter function to remove, from another page in the navigation.
MessagingCenter.Subscribe<RouteElement>(this, "Refresh",(sender) =>
{
RouteElement r = (RouteElement)sender;
rOC.Remove(r);
}
And now the button of the second RouteElement is green, even though it's supposed to be blue. Any help is much appreciated!
This "bug" happens only on Android with the newest package of Xamarin.Forms
<package id="Xamarin.Forms" version="2.3.3.193" targetFramework="monoandroid70" />
It works fine on Android with this package of Xamarin.Forms
<package id="Xamarin.Forms" version="2.2.0.31" targetFramework="monoandroid70" />
Are you defining a ListViewCachingStrategy for your ListView? You could try either:
_listView = new ListView(ListViewCachingStrategy.RecycleElement);
or
_listView = new ListView(ListViewCachingStrategy.RetainElement);
The ListView may be incorrectly reusing the color (but not the text/content) from the old cell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With