Xamarin Forms CustomCell in List Button Does background binding not work?

I have a general Xamarin.Forms project and the problem exists only on Android. My problem is that I have a list, and when I click a button in my custom cell, it changes color (from blue to green). Then I press another button that opens another page, and when I close this page, the item is removed from the list. But now the element below the shot has a green button, not a blue one. Here is an example:

  • The first image. When displaying a ListView with a custom cell inside containing information and two buttons, note that they are blue.

enter image description here

  1. The second image. Showing that I pressed the first button, and now it has turned green.

enter image description here

  1. Thrid image - Displays the page that is pressed when I press the second button.

enter image description here

  1. The fourth image. Now I clicked the “Bekræft" button on the image before, and the message was sent to the list page to remove the RouteElement from the list (and so it is). But now the first button is green, even if it has not been pressed.

enter image description here

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)); } } 

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 function of the MessagingCenter to delete from another page in the navigation.

 MessagingCenter.Subscribe<RouteElement>(this, "Refresh",(sender) => { RouteElement r = (RouteElement)sender; rOC.Remove(r); } 

And now the second RouteElement button is green, although it should be blue. Any help is much appreciated!

This "error" only occurs on Android with the latest Xamarin.Forms package

 <package id="Xamarin.Forms" version="2.3.3.193" targetFramework="monoandroid70" /> 

It works great on Android with this Xamarin.Forms package.

 <package id="Xamarin.Forms" version="2.2.0.31" targetFramework="monoandroid70" /> 
+6
source share
2 answers

Are you defining a ListViewCachingStrategy for your ListView? You may try:

 _listView = new ListView(ListViewCachingStrategy.RecycleElement); 

or

 _listView = new ListView(ListViewCachingStrategy.RetainElement); 

ListView may incorrectly reuse color (but not text / content) from the old cell.

+2
source

It works in 2.2x, but does not work in 2.3x.
There are many bugs in 2.3x that can be read here.

https://forums.xamarin.com/discussion/77854/xamarin-forms-2-3-3-193/p2

The developers suggest using 2.2x due to the large number of bugs. I suggest the same thing.

0
source

Source: https://habr.com/ru/post/1015177/


All Articles