I have a code which is download a JSON file and binding it to a CollectionView source.
It's showing as expected in debug. When I build the application and run it stand alone it's showing 10 empty boxes in my collection view (10 as that's my expected JSON result limit). I can click the 10 empty grids but no text is showing. It does in debug
what on earth is wrong?
Code:
public partial class PlayerListViewModel : ObservableObject
{
[ObservableProperty]
public string text = "";
public PlayerListViewModel() {
PlayerListItems = new ObservableCollection<Player>();
}
[ObservableProperty]
ObservableCollection<Player> playerListItems;
[RelayCommand]
public void View()
{
}
HttpClient _httpClient = new HttpClient();
string _url = "https://mydomain.com/all-players.php";
[RelayCommand]
async Task getPlayersAPI()
{
PlayerListItems.Clear();
var response = await _httpClient.GetAsync(_url);
var responseString = await response.Content.ReadAsStringAsync();
var playerListItems = JsonSerializer.Deserialize<List<Player>>(responseString);
Text = responseString.ToString();
foreach (var item in playerListItems)
{
PlayerListItems.Add(item);
}
}
Code:
<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>