Displaying collections
It is common to have a collection of data and to want to display it in a list, allowing the user to select one or more items and then do some work with those items. There are a couple of ways to do this in .NET MAUI, but the most common (and best) is to use CollectionView
.
To see this at work, examine Preferences.xaml
along with its code-behind Preferences.xaml.cs
and ViewModel
, PreferencesViewModel.cs
. We will use this page extensively as we build Forget Me Not, but let’s start slowly.
Our goal is to create a list of the user’s preferences (shirt size, music genre, and so on). For that, we’ll use CollectionView
, and we’ll, of course, need a collection to view. The page, when completed, will look much like this:
Figure 5.4 – The Preferences page
Rather than each line being a unique object, we’ll create one type that we can show repeatedly. In the Model
folder, create a Preference
class: