C
C#9mo ago
Brady Kelly

MAUI ContentPage binding not reflecting in child CollectionView on Android

I have this ContentPage in my MAUI app that targets only Android and iOS.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:Cts.Maui.Dx.Models"
x:Class="Cts.Maui.Dx.ApplicationListPage">
<ContentPage.BindingContext>
<models:ApplicationListViewModel x:Name="ViewModel" />
</ContentPage.BindingContext>
<ContentPage.Content>
<CollectionView ItemsSource="{Binding Data}" x:Name="CollectionView">
<CollectionView.Header>
<Label Text="Header" />
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Row="0" Text="{Binding LearnerDesc}" />
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding Originator}" />
<Label Grid.Column="1" Text="{Binding StartDate}" />
<Label Grid.Column="2" Text="{Binding IsLocked}" />
</Grid>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage.Content>
</ContentPage>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:Cts.Maui.Dx.Models"
x:Class="Cts.Maui.Dx.ApplicationListPage">
<ContentPage.BindingContext>
<models:ApplicationListViewModel x:Name="ViewModel" />
</ContentPage.BindingContext>
<ContentPage.Content>
<CollectionView ItemsSource="{Binding Data}" x:Name="CollectionView">
<CollectionView.Header>
<Label Text="Header" />
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Row="0" Text="{Binding LearnerDesc}" />
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding Originator}" />
<Label Grid.Column="1" Text="{Binding StartDate}" />
<Label Grid.Column="2" Text="{Binding IsLocked}" />
</Grid>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage.Content>
</ContentPage>
When I just populate the viewmodel in the page Loaded event, the collection view doesn't get bound to the viewmodel's collection, I only see the Header text. When I explicitly bind a viewmodel instance to the collection view's ItemSource, I see the items from the viewmodel:
var stream = await FileSystem.OpenAppPackageFileAsync("appList.json");
var streamReader = new StreamReader(stream);
var json = await streamReader.ReadToEndAsync();
// Just loading the page viewmodel doesn't work.
// ViewModel.LoadFromJson(await streamReader.ReadToEndAsync());
var model = new ApplicationListViewModel();
model.LoadFromJson(json);
CollectionView.ItemsSource = model.Data;
var stream = await FileSystem.OpenAppPackageFileAsync("appList.json");
var streamReader = new StreamReader(stream);
var json = await streamReader.ReadToEndAsync();
// Just loading the page viewmodel doesn't work.
// ViewModel.LoadFromJson(await streamReader.ReadToEndAsync());
var model = new ApplicationListViewModel();
model.LoadFromJson(json);
CollectionView.ItemsSource = model.Data;
I am raising PropertChanged for the Data property in the viewmodel:
public IList<LicenseApplicationDto>? Data { get; private set; }

public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public void LoadFromJson(string json)
{
Data = JsonSerializer.Deserialize<List<LicenseApplicationDto>>(json) ?? throw new InvalidOperationException("Deserialized to null");
OnPropertyChanged();
}
public IList<LicenseApplicationDto>? Data { get; private set; }

public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public void LoadFromJson(string json)
{
Data = JsonSerializer.Deserialize<List<LicenseApplicationDto>>(json) ?? throw new InvalidOperationException("Deserialized to null");
OnPropertyChanged();
}
To start with, besides them being separate libraries, why should the Android and Windows CollectionView and or ContentPage bind differently?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server