Application in break mode after clearing ListView items.

KrahosKrahos ITMember ✭✭

Hello everyone, I have a ListView which ItemsSource is bound to an ObservableCollection. The ItemTemplate is a custom ViewCell, which contains a horizontal StackLayout in which I have an Entry and multiple Switches (the number is decided at runtime by the user) which are bound to an array of bools in the ViewModel. The user can add items to the ObservableCollection by pressing a button. I would like to also let the user decide to clear the ListView of its items and start over from another button but whenever i try to do this, the application goes into break mode due to a non specificated unhandled exception. I've tried to use objects.Clear () (objects is the ObservableCollection), to use a foreach to remove each of the objects one by one, to set the ListView's ItemsSource to null, but the result is always the same. What am i missing?

Thanks for the attention and for the eventual help,

Krahos.

Posts

  • JohnHardmanJohnHardman GBUniversity mod

    @Krahos - Post the relevant code, and details of the "non specificated unhandled exception" (check in the output window of your IDE, or add an unhandled exception handler so that you can get the details from there). I assume this happens on Android, iOS and UWP, so you should be able to capture useful information about the exception when debugging on one of those platforms.

  • KrahosKrahos ITMember ✭✭

    @JohnHardman Sadly the problem is that the exception appears to happen in external code as visual studio says:
    "Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)."

    However i'll try to be more specific and to show the code that leads to this error.
    ListView initialization in the code behind:
    objectsList.ItemsSource = billsViewModel.Objects; objectsList.ItemTemplate = new DataTemplate(typeof (ObjectCell));
    I modify how many Switches have to be contained in the ViewCell like this:
    objectsList.ItemTemplate.SetValue(ObjectCell.NumberOfPeopleProperty, numberOfPeople);

    This is how i bind the content of the ViewCell with variables in the ViewModel
    ` private static void OnNumberOfPeopleChanged(BindableObject Bindable, object OldValue, object NewValue)
    {
    ObjectCell objectCell = (ObjectCell)Bindable;
    int newValue = (int)NewValue;

            if (objectCell.ObjectChecks.Children.Count != 0)   //ObjectChecks is a StackLayout defined in the xaml file
            {
                objectCell.ObjectChecks.Children.Clear();
            }
            Switch tempSwitch;
            for (int i = 0; i < newValue; ++i)
            {
                tempSwitch = new Switch
                {
                    HorizontalOptions = LayoutOptions.CenterAndExpand
                };
    
                string tmpstring = "PayingPeople [" + i + "]";
                tempSwitch.SetBinding(Switch.IsToggledProperty, tmpstring);
                tempSwitch.Toggled += objectCell.OnSwitchToggled;
    
                objectCell.ObjectChecks.Children.Add(tempSwitch);
            }
        }
    
        public static BindableProperty NumberOfPeopleProperty = BindableProperty.Create(
        propertyName: "NumberOfPeople",
        returnType: typeof(int),
        declaringType: typeof(ObjectCell),
        defaultValue: 0,
        defaultBindingMode: BindingMode.TwoWay,
        propertyChanged: OnNumberOfPeopleChanged);
    
        public int NumberOfPeople {
            get { return (int)GetValue(NumberOfPeopleProperty); }
            set { SetValue(NumberOfPeopleProperty, value); }
        }
    

    `
    When I want to clear the ListView i simply call Clear() in the ViewModel.

Sign In or Register to comment.