Xamarin在Listview按钮背景绑定中的CustomCell不起作用?

时间:2017-02-22 10:35:30

标签: xamarin xamarin.android xamarin.forms

我有一个共享的Xamarin.Forms项目,这个问题只存在于Android上。我的问题是,我有一个listview,当我点击我的customcell中的一个按钮时,它会改变颜色(从蓝色变为绿色)。然后我点击另一个按钮打开另一个页面,当我关闭该页面时,该项目将从列表视图中删除。但现在删除的项目下面的项目有一个绿色按钮,而不是蓝色。 这是一个例子:

  1. 第一张图片 - 显示带有包含信息和2个按钮的自定义单元格的ListView,请注意它们是蓝色的。
  2. enter image description here

    1. 第二张图片 - 显示我已按下第一个按钮,现在变为绿色。
    2. enter image description here

      1. Thrid image - 显示按下第二个按钮时推送的页面。
      2. enter image description here

        1. 第四张图片 - 现在我已经点击了之前图像上的“Bekræft”按钮,并且已将消息发送到Listview页面以从列表中删除RouteElement(所以它确实如此)。但是现在第一个按钮是绿色的,即使它没有被按下。
        2. enter image description here

          RouteElement模型。

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

          然后我调用此MessagingCenter功能从导航中的另一个页面中删除。

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

          现在第二个RouteElement的按钮是绿色的,即使它应该是蓝色的。任何帮助深表感谢!

          这个“bug”仅在Android上发布,包含最新的Xamarin.Forms包

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

          使用这个Xamarin.Forms包

          在Android上运行正常
          <package id="Xamarin.Forms" version="2.2.0.31" targetFramework="monoandroid70" />
          

2 个答案:

答案 0 :(得分:2)

您是否为ListView定义了ListViewCachingStrategy?您可以尝试:

_listView = new ListView(ListViewCachingStrategy.RecycleElement);

_listView = new ListView(ListViewCachingStrategy.RetainElement);

ListView可能错误地重用旧单元格中的颜色(但不重复文本/内容)。

答案 1 :(得分:0)

适用于2.2x但不适用于2.3x 2.3x有很多错误可以在这里阅读

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

由于存在大量漏洞,开发人员建议使用2.2x。我建议一样。