HomeiOS Developmentios - Initially chosen merchandise in MAUI CollectionView is not highlighted

ios – Initially chosen merchandise in MAUI CollectionView is not highlighted


I am programmatically initializing the choice of an merchandise in a CollectionView earlier than rendering it. On iOS, the UI exhibits nothing chosen when the CollectionView is first rendered although I bind SelectedItem to the property that’s programmatically initialized to level to one of many CollectionView objects. After I click on on an merchandise, the choice of objects works as anticipated. This all works as I count on on Android.

Right here is my easy ViewModel for reproducing this challenge:


namespace CollectionViewRepro
{
    public partial class CollectionViewData : ObservableObject
    {
        public Checklist<string> CollectionViewElements { get; non-public set; }

        [ObservableProperty]
        non-public string chosen;

        public CollectionViewData()
        {
            CollectionViewElements = new()
            {
                " 1 ",
                " 2 ",
                " 3 "
            };

            Chosen = CollectionViewElements[1];
        }
    }
}

Right here is the XAML:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:collectionviewrepro="clr-namespace:CollectionViewRepro"
             x:DataType="collectionviewrepro:CollectionViewData"
             x:Class="CollectionViewRepro.MainPage">

    <VerticalStackLayout
        Margin="10,30,10,0"
        HorizontalOptions="Middle">

        <Label Textual content="Pattern Label - Centered" HorizontalTextAlignment="Middle"/>

        <CollectionView ItemsSource="{Binding CollectionViewElements}" HorizontalOptions="Middle" SelectionMode="Single"
                        SelectedItem="{Binding Chosen}" HeightRequest="20">

            <CollectionView.ItemsLayout>
                <LinearItemsLayout Orientation="Horizontal"/>
            </CollectionView.ItemsLayout>
         </CollectionView>

        <HorizontalStackLayout Margin="0,20,0,0">
            <Label Textual content="That is the chosen merchandise: "/>
            <Label Textual content="{Binding Chosen}"/>
        </HorizontalStackLayout>


    </VerticalStackLayout>
</ContentPage>

Right here is the code-behind:

public partial class MainPage : ContentPage
{
    non-public CollectionViewData vm { get; set; }

    public MainPage()
    {
        vm = new CollectionViewData();
        InitializeComponent();
        BindingContext = vm;
    }
}

Here’s what the preliminary rendering seems to be like on an Android emulator:

enter image description here

On an iPhone emulator:

enter image description here

Notice that the textual content beneath the CollectionView within the iOS picture verifies that the Chosen property does level to “2” when the web page is rendered, however the 2 within the CollectionView is not highlighted. If I click on on any of the objects, it’s then highlighted and “That is the chosen merchandise:” exhibits the right merchandise. So, I do know the Chosen property is being outlined correctly and the TwoWay Binding of the CollectionView SelectedItem is working correctly.

This positively looks like a bug to me, nevertheless, I would love to listen to completely different views or prompt workarounds. I’ve thought of giving Syncfusion’s ListView a go.

Please forgive this very temporary rant… I am getting fairly pissed off and discouraged by the continual stream of bugs and inconsistencies throughout Android and iOS which have delayed my releases by many weeks.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments