5

I have the following

<CollectionView Margin="5" ItemsSource="{Binding Subjects}">
    <CollectionView.ItemsLayout>
        <GridItemsLayout Orientation="Vertical" Span="2" />
    </CollectionView.ItemsLayout>
    <CollectionView.EmptyView>
        <ContentView>
            <Label Text="No subjects entered yet..." />
        </ContentView>
    </CollectionView.EmptyView>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Border MaximumWidthRequest="300">
                <Border.StrokeShape>
                    <RoundRectangle CornerRadius="10" />
                </Border.StrokeShape>
                <Border.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type vm:MainPageViewModel}}, Path=DeleteSubjectCommand}" CommandParameter="{Binding .}" />
                </Border.GestureRecognizers>
                <Label Margin="5" Text="{Binding .}" />
            </Border>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

The problem is that the <Label Margin="5" Text="{Binding .}" /> is blank in release in my .NET MAUI Android application while in debug, it is populated and the gesture recogniser works. When I add items to the list, the correct number of items appears so it leads me to think that there is something happening with the label highlighted above.

How do I solve this?

3
  • This "Subjects" is ObservableCollection with String used as generic type, right? There are many issues with CollectionView, but posting some C# code around your XAML wont hurt. Commented Jan 30, 2023 at 11:23
  • * If you do ... Text="This is a test" />, does the text show? * If you remove the border and its recognizer, so datatemplate is simply <Label ... />, does the bound text show? * Add c# code: Subjects declaration, code that sets Subjects or adds its items * is that code called from constructor? OnAppearing? inside an async method? Commented Jan 30, 2023 at 20:31
  • I did the second thing. And still wouldn't appear. This is a test would appear in debug and not in release mode. Commented Jan 31, 2023 at 13:01

2 Answers 2

11

Replace:

<DataTemplate>

With:

<DataTemplate x:DataType="{x:Type x:String}">

Do not ask me why.

Sign up to request clarification or add additional context in comments.

6 Comments

I'd really love to ask why😂
@TanakaMawere I do not know. I have few releases already, and I have never faced your problem before. The thing that I noticed, is that I always specify the DataType of my templates. Removed the DataTypes. Tested in release, some of my CollectionViews became blank. Set the background, it turns out they are there, the items are not loading. Change to debug - items get rendered again. I could not believe it.
Please, just give it a try.
Can't believe it. This is still true to this day (19th Jan 2024). With .net 8 and all the "new" stuff. Makes no sense. Thank your for that answer, it helped me just as well !
By the way, I created a bug ticket on dotnet maui Repo thanks to this answer; as I'm having the exact same issue and your suggestion is a valid workaround for this. Thankls again @H.A.H. We'll see if that helps.
|
0

I've ran into the same issue, the more generic solution if your collection view binding type is not known at compile time is to bind to x:DataType="{x:Type x:Object}"

This is useful is your making components that bind to collections and can't hard-code the datatype in advance

<CollectionView.ItemTemplate>
    <DataTemplate x:DataType="{x:Type x:Object}">
        <Grid x:Name="OuterHostGrid" ColumnDefinitions="*, 60">
            <Label
                FontAttributes="None"
                FontFamily="PoppinsMedium"
                FontSize="13"
                Text="{x:Binding .}"
                TextColor="{AppThemeBinding Light=Black,
                                            Dark=White}"
                VerticalOptions="Center" />
           </Grid>
     </DataTemplate>
</CollectionView.ItemTemplate>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.