Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms CustomCell in Listview Button Background binding not working?

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:

  1. First image - Showing the ListView with a customcell inside containing information and 2 buttons, notice that they are blue.

enter image description here

  1. Second image - Showing that I have pressed the first button, and it has now turned green.

enter image description here

  1. Thrid image - Showing the page that is pushed when I press the second button.

enter image description here

  1. Fourth image - Now I have clicked the "Bekræft" button on the image before, and the Message has been sent to the Listview page to remove the RouteElement from the list (and so it does). But now the first button is green, even though it has not been pressed.

enter image description here

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" />
like image 369
Jesper Højer Avatar asked Feb 22 '17 10:02

Jesper Højer


1 Answers

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.

like image 75
Alex West Avatar answered Nov 01 '22 08:11

Alex West