locked
Listview item event multiple events fired RRS feed

  • Question

  • User325448 posted

    I am using Listview with an observable collection. But I am getting weird behaviour when I am going from Navigation Page A -> B -> C -> B

    Page B listview datetemplate DATATEMPLATE <\ListView.ItemTemplate> <\DataTemplate> <\ViewCell> <\StackLayout Orientation="Horizontal" VerticalOptions="FillAndExpand"> <\Label x:Name="Color" BackgroundColor="{Binding ListViewColor}" HorizontalOptions="Start" WidthRequest="10" VerticalOptions="FillAndExpand" Margin="0,3,0,3"/> <\StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand"> <\Label x:Name="Title" Text="{Binding ListViewTitle}"/> <\Label x:Name="GoodsKind" Text="{Binding ListViewGoodsKind}" /> <\Label x:Name="Treatment" Text="{Binding ListViewTreatment}" /> <\Switch x:Name="Switch" IsToggled="{Binding ListViewChecked, Mode=TwoWay}" Toggled="Switch_Toggled" HorizontalOptions="End" />

    PAGE B model from data template public bool ListViewChecked { get { return _Checked; }

            set
            {
                if (this._Checked == value)
                {
                    return;
                }
                _Checked = value;
    
                if (PropertyChanged != null)
                {
                    OnPropertyChanged(nameof(ListViewChecked));
                }
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            this.PropertyChanged?.Invoke(this,
              new PropertyChangedEventArgs(propertyName));
        }
    

    Step B On listview, ItemSelected page C is loaded but before the Listview Item Selected event is fired the child event is fired case Switch IsToggled=True SwitchToggled is fired before Item Selected event is fired. case Switch IsToggled=False switchtoggled is not fired only Item Selected event is fired.

    STEP B -> C -> B I navigate from page B to page C and then back to Page B case When I click on Switch to enable/disable Switch IsToggled event is fired three times Repeat the step B-> C -> B Switch IsToggled event is fired four times ,............................. and so on

    Is this some kind of bug in Listview or am I doing something wrong with my code?

    Tuesday, May 23, 2017 10:17 AM

Answers

  • User42253 posted

    OK, I didn't check that one. I think it would useful if you could make a simple solution with the test case and complete bug #47602.

    Good luck for us ;-)

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 25, 2017 6:12 AM

All replies

  • User3516 posted

    Sounds like you are adding the event handler multiple times, but that is hard to tell. Show your whole code for the involved pages.

    Tuesday, May 23, 2017 11:14 AM
  • User325448 posted

    ` PAGE A XAML

    PAGE A c#

    #

    using iDOS.DataModel; using Xamarin.Forms; using Xamarin.Forms.Xaml;

    namespace iDOS.DModelTestConnect { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class DeliveryPage : ContentPage { private int DNAL_ID;

        private OrderDataAccess dataAccess;
        public DeliveryPage (int DNAL_ID)
        {
            InitializeComponent ();
            this.DNAL_ID = DNAL_ID;
            this.dataAccess = OrderDataAccess.getOderDataAccess();
            this.dataAccess.selectDelivery(DNAL_ID);
            this.BindingContext = this.dataAccess;
        }
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
        }
    
        async void ListViewDelivery_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            if (this.IsEnabled)
            {
                this.IsEnabled = false;
                ListViewDelivery.SelectedItem = null;
                DbModelDOSDOS item = e.SelectedItem as DbModelDOSDOS;
                await Navigation.PushAsync(new ShipmentPage(item.DNAL_ID, item.DDOS_ID));
                this.IsEnabled = true;
            }
        }
    }
    

    }

    PAGE B XAML

    #

    PAGE B C#

    #

    using iDOS.DataModel; using iDOS.Views; using System; using System.Linq; using Xamarin.Forms; using Xamarin.Forms.Xaml;

    namespace iDOS.DModelTestConnect { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class ShipmentPage : ContentPage { private int DNALID; private int DDOSID; private OrderDataAccess dataAccess; public ShipmentPage(int DNALID, int DDOSID) { InitializeComponent(); this.DNALID = DNALID; this.DDOSID = DDOSID; this.dataAccess = OrderDataAccess.getOderDataAccess(); this.dataAccess.selectShipment(DNALID, DDOSID); this.BindingContext = this.dataAccess; }

        protected override void OnAppearing()
        {
            base.OnAppearing();
        }
    
        public void Switch_Toggled(object sender, ToggledEventArgs e)
        {
            if (sender.GetType() == typeof(Switch))
            {
                int count = this.dataAccess.Posiljke.Where(X => X.ListViewChecked == true).Count();
    
                if (count > 0)
                {
                    ButtonSign.Text = "Sign " + count;
                    ButtonSign.IsVisible = true;
                } else
                {
                    ButtonSign.IsVisible = false;
                }
    
                DisplayAlert("Obvestilo", "Izbrano " + count, "OK");
            }
        }
    
        async void ListViewDelivery_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            if (this.IsEnabled)
            {
                this.IsEnabled = false;
                ListViewShipment.SelectedItem = null;
                DbModelDOSDOP item = e.SelectedItem as DbModelDOSDOP;
                await Navigation.PushAsync(new KolijiPage(item.DNAL_ID, item.DDOS_ID, item.DDOP_ID));
                this.IsEnabled = true;
            }
        }
    
        protected override bool OnBackButtonPressed()
        {
            base.OnBackButtonPressed();
            return false;
        }
    
        async void ButtonSign_Clicked(object sender, EventArgs e)
        {
            if (this.IsEnabled)
            {
                this.IsEnabled = false;
                await Navigation.PushAsync(new SignaturePage());
                this.IsEnabled = true;
            }
        }
    }
    

    }

    PAGE C XAML

    #

    PAGE C C#

    #

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

    using Xamarin.Forms; using Xamarin.Forms.Xaml;

    namespace iDOS.DModelTestConnect { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class KolijiPage : ContentPage { private int DNALID; private int DDOSID; private int DDOPID; private OrderDataAccess dataAccess; public KolijiPage(int DNALID, int DDOSID, int DDOPID) { InitializeComponent(); this.DNALID = DNALID; this.DDOSID = DDOSID; this.DDOPID = DDOPID; this.dataAccess = OrderDataAccess.getOderDataAccess(); }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            this.dataAccess.selectKoliji(DNAL_ID, DDOS_ID, DDOP_ID);
            this.BindingContext = this.dataAccess;
        }
    
        private void ListViewKoliji_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            ListViewKoliji.SelectedItem = null;
        }
    }
    

    }

    MODEL B

    #

    using Newtonsoft.Json; using SQLite; using System.ComponentModel; using Xamarin.Forms;

    namespace iDOS.DataModel { public class DbModelDOSDOP : INotifyPropertyChanged { public string ListViewTitle { get { return (PODSYNC > 0) ? "! " + DDOPNAZ : DDOP_NAZ; } }

        public string ListViewGoodsKind
        {
            get
            {
                return DDOP_VBL;
            }
        }
    
        public string ListViewTreatment
        {
            get
            {
                return (DDOP_OBR == "1") ? "Prejem" : 
                       (DDOP_OBR == "2") ? "Dostava" : "Napaka";
            }
        }
    
        private Color _color;
        public Color ListViewColor
        {
            get
            {
                return _color;
            }
    
            set
            {
                _color = value;
                OnPropertyChanged(nameof(ListViewColor));
            }
        }
    
        private bool _Checked;
        public bool ListViewChecked
        {
            get
            {
                return _Checked;
            }
    
            set
            {
                if (this._Checked == value)
                {
                    return;
                }
                _Checked = value;
    
                if (PropertyChanged != null)
                {
                    OnPropertyChanged(nameof(ListViewChecked));
                }
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            this.PropertyChanged?.Invoke(this,
              new PropertyChangedEventArgs(propertyName));
        }
    }
    

    } `

    Tuesday, May 23, 2017 11:55 AM
  • User42253 posted

    Hi, I've the same issue with XF with at least two apps. On UWP the listView.ItemSelected fires two times, whereas the app is working fine on Android.

    I hadn't yet the time to dig into the origines, but it could be related to: * the last preview of Xamarin.Forms (2.3.5.239-pre3) * or the fact that I'm using VS2017 preview

    As far as I recall that are the mayor changes of the last days. @iplus do you updated something in your config theses days?

    Tuesday, May 23, 2017 2:00 PM
  • User325448 posted

    @MichaelEngelmann said: Hi, I've the same issue with XF with at least two apps. On UWP the listView.ItemSelected fires two times, whereas the app is working fine on Android.

    I hadn't yet the time to dig into the origines, but it could be related to: * the last preview of Xamarin.Forms (2.3.5.239-pre3) * or the fact that I'm using VS2017 preview

    As far as I recall that are the mayor changes of the last days. @iplus do you updated something in your config theses days?

    Hi, I've tried it on last stable version, on pre3 and 3.0-pre.

    Wednesday, May 24, 2017 4:01 AM
  • User325448 posted

    I just tried to run the app on windows 10 and when I click on a row in listview 4 events on the item selected are fired. And child events are also fired even if they are not clicked.

    When I click on a row ItemSelected is fired First event SelectedItem != null (I am getting selected object) Second event SelectedItem == null (probably because it is set to null ListViewShipment.SelectedItem = null) Third event SelectedItem != null (I am getting the selected object again) Fourth event SelectedItem == null (probably because it is set to null in Third event)

        async void ListViewDelivery_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            DbModelDOSDOP item = e.SelectedItem as DbModelDOSDOP;
            if (item != null)
            {
                ListViewShipment.SelectedItem = null;        
                await Navigation.PushAsync(new KolijiPage(item.DNAL_ID, item.DDOS_ID, item.DDOP_ID));
            }
        }
    
    Wednesday, May 24, 2017 5:57 AM
  • User66163 posted

    Hi @iplus

    Just to point you in the right direction:

    Please consider exactly how useful is it to have one x:Name on a control defined in a template.

    At runtime when the List view items are generated from the template, that would result in multiple instances of a control with the same x:Name.

    Have a look at https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/

    Hope this helps,

    Tim

    Wednesday, May 24, 2017 8:25 AM
  • User42253 posted

    Hi @iplus , I made some more investigation using a test project with only one page:

      class TestPage : ContentPage
      {
          public TestPage()
          {
             // 'Xamarin.Forms.2.3.4.247' --> OK
             // 'Xamarin.Forms.2.3.5.233-pre1' --> KO
             // 'Xamarin.Forms 2.3.5.239-pre3' --> KO
    
             var list = new List<string>();
             list.Add("-1-");
             list.Add("-2-");
             list.Add("-3-");
    
             var lv = new ListView();
             lv.ItemSelected += Lv_ItemSelected;
             lv.ItemsSource = list;
    
             Content = lv;
          }
    
          private void Lv_ItemSelected(object sender, SelectedItemChangedEventArgs e)
          {
             Debug.WriteLine("Lv_ItemSelected");
          }
       }
    

    As stated in the comments it worked fine on UWP until Xamarin.Forms 2.3.4.247. If you downgrade the Nuget don’t forget to make a clean for the solution before compiling again. I’ll check if there is already a bug filed. Michael

    Wednesday, May 24, 2017 1:56 PM
  • User42253 posted

    The was already a bug report filed in the past https://bugzilla.xamarin.com/show_bug.cgi?id=47602

    Wednesday, May 24, 2017 2:15 PM
  • User325448 posted

    I checked 2.3.4.247 on UWP works fine but on android the problem still exists. When clicking on a row the event of child (Switch) is also triggered and for each navigation from PAGE B -> PAGE C -> PAGE B Switch event is increased by one

    Thursday, May 25, 2017 4:28 AM
  • User42253 posted

    OK, I didn't check that one. I think it would useful if you could make a simple solution with the test case and complete bug #47602.

    Good luck for us ;-)

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, May 25, 2017 6:12 AM
  • User325448 posted

    @MichaelEngelmann said: OK, I didn't check that one. I think it would useful if you could make a simple solution with the test case and complete bug #47602.

    Good luck for us ;-)

    Thank you very much. I just posted example on https://bugzilla.xamarin.com/show_bug.cgi?id=47602 and two videos demonstrating the bug

    Thursday, May 25, 2017 6:57 AM
  • User325448 posted

    @MichaelEngelmann said: OK, I didn't check that one. I think it would useful if you could make a simple solution with the test case and complete bug #47602.

    Good luck for us ;-)

    I searched Bugzilla and I found the topic discussing the bug https://bugzilla.xamarin.com/show_bug.cgi?id=45722

    Thursday, May 25, 2017 9:20 AM
  • User246263 posted

    Did anyone get the solution?

    Wednesday, July 5, 2017 9:01 AM
  • User97480 posted

    They considered solved in 2.4.0.275-pre3, but still buggy:

    https://bugzilla.xamarin.com/show_bug.cgi?id=44886

    Monday, September 25, 2017 1:22 PM