maui icon indicating copy to clipboard operation
maui copied to clipboard

Windows and iOS: CollectionView selection does not display selection when page appearing

Open hbraasch opened this issue 4 years ago • 12 comments

Description

The attached repro project demonstrates the issue. It works correctly on Android.

When setting the CollectionView selected item to a non-null display item during the OnAppearing event, the selection stays transparent and does not change colour.

Below is what should be displayed (Android). Play video...

https://user-images.githubusercontent.com/7484186/177675109-4df79e09-6a56-4fa7-b28a-6297f410e791.mp4

Below is what appears for (iOS). Play video...

https://user-images.githubusercontent.com/7484186/177675204-4a0c46ac-0ce6-4bda-8790-1f10da20891c.mp4

Below is what appears for Windows after startup. Play video...

https://user-images.githubusercontent.com/7484186/177675645-1d858b78-e0db-4b3a-bfaa-9962f97f32a8.mp4

Steps to Reproduce

  1. Open repro project from Github: https://github.com/hbraasch/LayoutTester.git
  2. Run each project and observe the differences.

Version with bug

6.0.400 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Windows

Affected platform versions

iOS 15.4, Windows 10.0.19041.0

Did you find any workaround?

None

Relevant log output

No response

hbraasch avatar Jul 07 '22 02:07 hbraasch

I'm also seeing this on iOS, but it isn't just limited to page appearing. Any time when the selected item is set when the view is being hidden or shown risks the selection appearance not being shown. It is noticeable even when using the Visual State Manager to customize the selection background color.

Also applies on Mac Catalyst.

nCastle1 avatar Sep 09 '22 03:09 nCastle1

+1

minglu avatar Jan 23 '23 19:01 minglu

+1 on Windows, 8 months later. Have Microsoft actually got more than a couple of devs working on MAUI, they just seem to get ignored for months on end? It's the same with the issue of the on-screen keyboard not appearing on touch devices when an input field is focused, I'm sick of having to use workarounds to get around all the missing or faulty basic functionality.

nickidentivue avatar Feb 23 '23 11:02 nickidentivue

Facing same issue on iOS in Visual Studio Version 17.5.4

HobDev avatar Apr 27 '23 12:04 HobDev

As a workaround I am assigning the selected items on page loaded event:

 this.Loaded += View_Loaded;
  private async void View_Loaded(object sender, EventArgs e)
    {
        await viewModel.AddItemsToSelectedItems();
    }

HobDev avatar Apr 27 '23 14:04 HobDev

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 6.0. Can repro this issue with sample project. https://github.com/hbraasch/LayoutTester.git Android: image iOS: image Windows: image

homeyf avatar Aug 08 '23 08:08 homeyf

Still no fix for over a year and a half ? We still have the problem with the .NET8 version :( It's complicated to make an application with a good user experience.

oximoron-fr avatar Dec 20 '23 22:12 oximoron-fr

I created a workaround that seems to work under Windows and Android (not tested iOS yet):

public static class CollectionViewExtensions {
    public static void EnableSelectedStateHack( this CollectionView self ) {
        self.SelectionChanged -= OnSelectionChanged;
        self.SelectionChanged += OnSelectionChanged;

        self.ChildAdded -= OnChildAdded;
        self.ChildAdded += OnChildAdded;
    }

    static void OnChildAdded( object? sender, ElementEventArgs e ) {
        Debug.Assert( sender != null );

        var s = ( CollectionView )sender;
        Debug.Assert( s != null );

        if( e.Element.BindingContext == s.SelectedItem ) {
            var ve = e.Element as VisualElement;

            if( ve != null ) {
                VisualStateManager.GoToState( ve, "Normal" );
                VisualStateManager.GoToState( ve, "Selected" );
            }
        }
    }
}

After calling the "InitializeComponent" method, you have to call "EnableSelectedStateHack" to enable the hack. I don't know of any way to do it automatically.

mchiz avatar Mar 29 '24 11:03 mchiz

Thank @mchiz!!! This worked very nicely. A couple of notes:

  1. You left OnSelectionChanged event registration in your code, that just needs to be deleted.
  2. In my case my s.SelectedItem was a string and the object comparison fails, so I needed to adjust the OnChildAdded code as follows (there is probably a more generic way to accomplish this, but I didn't try to figure it out):
    static void OnChildAdded(object? sender, ElementEventArgs e)
    {
        Debug.Assert(sender != null);

        var s = (CollectionView)sender;
        Debug.Assert(s != null);

        bool equal = false;
        if(e.Element.BindingContext is string s1 &&
           s.SelectedItem is string s2)
        {
            equal = s1 == s2;
        }
        else
        {
            equal = e.Element.BindingContext == s.SelectedItem;
        }
        if (equal)
        {
            var ve = e.Element as VisualElement;

            if (ve != null)
            {
                VisualStateManager.GoToState(ve, "Normal");
                VisualStateManager.GoToState(ve, "Selected");
            }
        }
    }

It's a pretty surprising Microsoft hasn't fixed this yet after 3 years of being reported, makes me want to move away from MAUI to be honest.

sgodin avatar Mar 06 '25 21:03 sgodin

@sgodin I'm so glad it helped you!

Yes, MAUI has a lot of issues like this, but overall it is worth it (for me, at least).

mchiz avatar Mar 06 '25 22:03 mchiz

This issue is not reproducible in the latest .NET MAUI version 9.0.90 on iOS 18.0 in both CV1 and CV2 and Windows. I have attached screenshot for your reference.

Windows iOS

HarishwaranVijayakumar avatar Aug 18 '25 07:08 HarishwaranVijayakumar

Hi @@hbraasch. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.