UWP XAML is throwing an unhandled exception when a collection is empty

Mahmudul Hasan 1 Reputation point
2019-12-06T06:31:54.917+00:00

I have two ObservableCollection and its bonded with two different ListView UI.

ObservableCollection deptList = new ObservableCollection();  
ObservableCollection teacherList = new ObservableCollection();  

User can add or remove items from the collection in the run time.

At some point, if either of the lists is empty, the app freezes and throws an unhandled exception {Windows.UI.Xaml.UnhandledExceptionEventArgs}

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION  
            UnhandledException += (sender, e) =>  
            {  
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();  
            };  
#endif  

Exception Message

Message = "Layout cycle detected. Layout could not complete.\r\nLayout cycle detected. Layout could not complete."

_message = "A cycle occurred while laying out the GUI."

Here is the code block for ListView UI -

C# code for events

private void depertmentList_SelectionChanged(object sender, SelectionChangedEventArgs e)  
{  

    if (depertmentList.SelectedItem == null)  
    {  
        removeDept.IsEnabled = false;  
        teacherListView.ItemsSource = teacherList;  
    }  
    else  
    {  
        removeDept.IsEnabled = true;  
        ObservableCollection t = new ObservableCollection();  
        Department temp = (Department)depertmentList.SelectedItem;  
        teacherListView.ItemsSource = from teacher in teacherList  
                                      where teacher.Dept == temp.Dept  
                                      select teacher;  
    }  
}  
private void teacherListView_SelectionChanged(object sender, SelectionChangedEventArgs e)  
{  
    if (teacherListView.SelectedItem == null)  
    {  
        removeTeacher.IsEnabled = false;  
    }  
    else  
    {  
        removeTeacher.IsEnabled = true;  
    }  
}  

Point to be noted, Teacher class derives from Department class. And if I remove a Department, all the teacher associated with that department gets removed too.

To prevent crashing the app, I had to add a hardcoded garbage item to the list which the user can remove later in run time.

And again if one of the lists is empty, the app crashes. But if there is at least one item in both lists, nothing happens.

Now, my question is How can I resolve this issue without adding any type of garbage item? I want to prevent crashing even if the lists are empty or null.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xiaodi Yan 876 Reputation points MVP
    2019-12-06T07:10:39.55+00:00

    I'm thinking if you could check these points:

    1. Did you use generic type for your ObservableCollection? Could you please provide your Teacher class and Department class? Also could you please provide the code of XAML file and how you set up the data-binding?
    2. Ideally, the ObservableCollection should be initialized as an empty list when you create the instance of the ViewModel. Then you just add or remove elements from the collection.
    3. Line 12 makes no sense. Not sure why you created a new collection but it is not used after that.
    4. If you already set the data-binding of the ListView, you could just operate the ObservableCollection, not assign the ItemsSource directly.

    My suggestion is:
    Use MVVM pattern and set up the data-binding for ListView controls. You could bind the properties such as ItemsSource and SelectedItem of the ListView. ObservableCollection is for the ItemsSource of the ListView. For SelectedItem, you should have other properties called SelectedDepartment and SelectedTeacher that implement INotifyPropertyChanged interface. When you remove items from ListViews, just update the ObservableCollection, SelectedDepartment and SelectedTeacher. The UI would be updated automatically.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.