Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by JovianTracker826

Why Do CollectionView Items Appear Empty in My Published .NET MAUI App?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I've built a small .NET MAUI application in Visual Studio that downloads a JSON file containing 10 players and loads them into a CollectionView. When debugging in the IDE, the players display correctly and the JSON shows in the editor; however, when running from the debug folder, I only see 10 empty outlines in the CollectionView without any IDs or Names, even though the JSON appears in the editor.

I followed this tutorial: https://www.youtube.com/watch?v=GgU4ulGYQEk

Here is the relevant code:

CSHARP
public partial class PlayerListViewModel: ObservableObject { [ObservableProperty] public string text = ""; public PlayerListViewModel() { PlayerListItems = new ObservableCollection<Player>(); } [ObservableProperty] ObservableCollection<Player> playerListItems; [RelayCommand] public void View() { } [RelayCommand] async Task getPlayersAPI() { HttpClient _httpClient = new HttpClient(); string _url = "https://mywebsite.com/all-players.php"; var response = await _httpClient.GetAsync(_url); var responseString = await response.Content.ReadAsStringAsync(); var playerListItems = JsonSerializer.Deserialize<List<Player>>(responseString); Text = responseString.ToString(); PlayerListItems.Clear(); foreach(var item in playerListItems) { PlayerListItems.Add(item); } } }
CSHARP
public class Player { public int Id { get; set; } public required string fullName { get; set; } public required string imageSrc { get; set; } public required string countryFlag { get; set; } }
JSON
[ { "Id": 577, "fullName": "Bob Smith", "imageSrc": "ProfileImage.webp", "countryFlag": "us.svg" }, { "Id": 744, "fullName": "George Kingford", "imageSrc": "ProfileImage.webp", "countryFlag": "ca.svg" } ]
XML
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyProject.PlayerList" Title="PlayerList" xmlns:viewmodel="clr-namespace:MyProject.ViewModel" x:DataType="viewmodel:PlayerListViewModel"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="100"></RowDefinition> <RowDefinition Height="200"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="50"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="300"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Button x:Name="getBtn" Text="Load" Command="{Binding getPlayersAPICommand}" Margin="10" HeightRequest="30" Grid.Column="1"></Button> <Editor x:Name="testData" Text="{Binding Text}" Margin="10" Grid.Column="1" Grid.Row="1"></Editor> <ScrollView Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" VerticalOptions="End" VerticalScrollBarVisibility="Always" > <CollectionView ItemsSource="{Binding PlayerListItems}" EmptyView="No players downlaoded" SelectionMode="Single" > <CollectionView.Header> <VerticalStackLayout> <Label Text="Players" HorizontalOptions="Center" FontAttributes="Bold" FontSize="18"></Label> </VerticalStackLayout> </CollectionView.Header> <CollectionView.ItemTemplate> <DataTemplate x:DataType="{x:Type viewmodel:PlayerListViewModel}"> <SwipeView> <SwipeView.RightItems> <SwipeItem Text="Edit" BackgroundColor="Green" /> </SwipeView.RightItems> <Grid Padding="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"></ColumnDefinition> <ColumnDefinition Width="auto"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="50*"></RowDefinition> <RowDefinition Height="50*"></RowDefinition> </Grid.RowDefinitions> <Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="1" HeightRequest="30" Source="{Binding Source, Source={x:Reference countryFlag}}"></Image> <Label Grid.Column="1" Grid.Row="0" Text="{Binding fullName}" FontAttributes="Bold" ></Label > <HorizontalStackLayout Grid.Column="1" Grid.Row="1"> <Label Text="ID: "></Label > <Label TextColor="Black" Text="{Binding Id}" ></Label > </HorizontalStackLayout> </Grid> </SwipeView> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> </ScrollView> </Grid> </ContentPage>

Based on feedback in the answers, it seems likely that the issue is caused by setting the DataTemplate's x:DataType to the PlayerListViewModel instead of the Player class, so the item bindings (like fullName and Id) are not found. Does anyone have further insights or solutions?

1

Comments 0

Answers 3

Views 58

3 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Below is one likely cause: the DataTemplate’s type is set to your view model rather than to a Player. This causes the bindings (like fullName and Id) to look for corresponding properties on the view model rather than on the individual Player items. In the IDE debug session the wrong binding might seem to “work” (or appear to) but when running from the debug folder the collection items show as empty because the binding context for each item isn’t what you expect.

What to Check

  • DataTemplate x:DataType:
    In your CollectionView’s ItemTemplate, you currently have:

    XML
    <DataTemplate x:DataType="{x:Type viewmodel:PlayerListViewModel}">

    Here, the DataTemplate is telling the XAML compiler that each item is a PlayerListViewModel‑instance. However, each item in your PlayerListItems collection is of type Player. Changing the DataTemplate x:DataType to Player (or whatever your Player model’s XML namespace alias is) will ensure that the bindings resolve to properties like fullName, Id, etc.

  • Correct Binding Context for DataTemplate:
    When the wrong type is used, the bindings in your item template (e.g., Text="{Binding fullName}") won’t find a matching property on the view model and will render as empty.

How to Fix

Assuming you have an XML namespace alias (for example, “model”) for your Player class, update the DataTemplate in your XAML to reference Player instead:

XML
<!-- Add an xmlns mapping if not already present --> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:model="clr-namespace:MyProject.Models" xmlns:viewmodel="clr-namespace:MyProject.ViewModel" x:Class="MyProject.PlayerList" x:DataType="viewmodel:PlayerListViewModel"> <Grid> <!-- Your other layout elements --> <ScrollView Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" VerticalOptions="End" VerticalScrollBarVisibility="Always"> <CollectionView ItemsSource="{Binding PlayerListItems}" EmptyView="No players downloaded" SelectionMode="Single"> <CollectionView.Header> <VerticalStackLayout> <Label Text="Players" HorizontalOptions="Center" FontAttributes="Bold" FontSize="18"/> </VerticalStackLayout> </CollectionView.Header> <CollectionView.ItemTemplate> <DataTemplate x:DataType="model:Player"> <SwipeView> <SwipeView.RightItems> <SwipeItem Text="Edit" BackgroundColor="Green"/> </SwipeView.RightItems> <Grid Padding="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"/> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="50*"/> <RowDefinition Height="50*"/> </Grid.RowDefinitions> <!-- Adjust the binding for Image as needed --> <Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="1" HeightRequest="30" Source="{Binding countryFlag}"/> <Label Grid.Column="1" Grid.Row="0" Text="{Binding fullName}" FontAttributes="Bold"/> <HorizontalStackLayout Grid.Column="1" Grid.Row="1"> <Label Text="ID: "/> <Label TextColor="Black" Text="{Binding Id}"/> </HorizontalStackLayout> </Grid> </SwipeView> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> </ScrollView> </Grid> </ContentPage>

Additional Notes

  • Ensure that the XML namespace (here model) is correctly mapped to the namespace where your Player class resides.
  • Double-check your JSON property names to ensure they exactly match your Player properties—it looks like they do.
  • Verify that the ObservableCollection “PlayerListItems” is correctly populated and not being shadowed by any local variables. In your code you’re using a local variable with the same name as the property. Since you clear the collection and then add the items one by one, this is acceptable; just be cautious with naming to avoid confusion.

This change should resolve the issue of empty player details in the CollectionView when running your application from the debug folder.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by NeptunianAstronaut277 1 month ago

0

Please set the x:DataType of the <DataTemplate> in the <CollectionView.ItemTemplate> as Player class not the PlayerListViewModel.

XML
xmlns:model="clr-namespace:MyProject.Model" .... <CollectionView.ItemTemplate> <DataTemplate x:DataType="{x:Type model:Player}"> <SwipeView>

No comments yet.

Answer by GalacticStargazer576 1 month ago

0

The problem is this line:

XML
<DataTemplate x:DataType="{x:Type viewmodel:PlayerListViewModel}">

It has, at least, two issues:

  1. It's recommended you don't use x:Type when specifying x:DataType
  2. The DataType for your collection item cannot be PlayerListViewModel since your code behind suggests that collection items are of type Player

The following is an example of what the corrected line could look like:

XML
<DataTemplate x:DataType="viewmodel:Player">

No comments yet.

Discussion

No comments yet.