Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VisualStateManager does not seem to work on Android #5449

Closed
david-maw opened this issue Mar 20, 2022 · 4 comments · Fixed by #6249
Closed

VisualStateManager does not seem to work on Android #5449

david-maw opened this issue Mar 20, 2022 · 4 comments · Fixed by #6249
Assignees
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView fixed-in-6.0.300-rc.2 Look for this fix in 6.0.300-rc.2! platform/android 🤖 s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@david-maw
Copy link

Description

Using a VisualStateManager to highlight selected items in a CollectionView works fine in Windows but not in Android. The sample one I'm using here is:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup Name="CommonStates">
        <VisualState Name="Normal" />
        <VisualState Name="Selected">
            <VisualState.Setters>
                <Setter Property="BackgroundColor" Value="Yellow" />
            </VisualState.Setters>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

This should highlight any selected items (if there are any) by drawing their background in yellow.

Steps to Reproduce

  1. Unzip MAUIListEntry-VisualState.zip
  2. Compile and run it on Windows
  3. Click on one of the list items, note its background changes to yellow
  4. click on the other one, note that its background changes to yellow and the one that was formerly yellow goes back to white
  5. Recompile and run the program on Android
  6. Repeat actions 3 & 4 and observe that the list item background does not change.

Version with bug

Preview 14 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 11, Windows 10

Did you find any workaround?

You can code around it for single selections using the SelectionChanged event to explicitly handle changing background color but I've no idea how general that solution is.

Relevant log output

No response

@david-maw david-maw added s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working labels Mar 20, 2022
@Amir-Care Amir-Care added the s/triaged Issue has been reviewed label Mar 21, 2022
@Amir-Care
Copy link

Verified repro with Version 17.2.0 Preview 2.0 [32208.484.main]
Sample project: #5449.zip

@Amir-Care Amir-Care added s/verified Verified / Reproducible Issue ready for Engineering Triage and removed s/triaged Issue has been reviewed s/needs-verification Indicates that this issue needs initial verification before further triage will happen labels Mar 21, 2022
@jsuarezruiz jsuarezruiz added area-controls-collectionview CollectionView, CarouselView, IndicatorView platform/android 🤖 labels Mar 21, 2022
@Redth Redth added this to the 6.0.300-rc.2 milestone Mar 21, 2022
@antonfirsov antonfirsov self-assigned this Apr 12, 2022
@antonfirsov
Copy link
Member

antonfirsov commented Apr 15, 2022

There are two issues here:

  1. VisualStateManager.GoToState is not hooked into the Android adapter code. This is easy to fix. However after doing so there is a bigger problem:
  2. "Switching back" to unselected state seems to be implemented by setting the property (in this case BackgroundColor) to null which doesn't do anything. This is essentially identical to Setting View.BackgroundColor = null doesn't reset the color on Android #5973 which is by design. Can anyone suggest how to address this?

This is where BackgroundColor's UnApply lands:

else
targetObject.ClearValue(Property, fromStyle);

Then in ClearValue it will set newValue == null:

object newValue = bpcontext.StyleValueSet ? bpcontext.StyleValue : property.GetDefaultValue(this);

@antonfirsov
Copy link
Member

antonfirsov commented Apr 22, 2022

@david-maw this is going to be essentially fixed with #6249 in the next release, but note that you have to define a default BackgroundColor for your grid, otherwise VisualStateManager won't be able to determine what color to switch back to after deselection:

<Grid ColumnDefinitions="*,Auto" BackgroundColor="White">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualState Name="Normal" />
<VisualState Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Yellow" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Grid.ColumnSpan="2" Margin="10,0,10,0"
RowDefinitions="Auto" ColumnDefinitions="*,*">
<Label Text="{Binding ItemName}" Grid.Column="0"
LineBreakMode="NoWrap"
FontSize="Large" />
</Grid>
</Grid>

This is by design, see #6183 and #1485 for more info.

@david-maw
Copy link
Author

Thanks for the fix @antonfirsov I can code around that constraint but it's a pain, there are a few consequences of that design decision that make migration from Xamarin harder, so I've raised Issue #6454 to track them. On the face of it that's an awkward design decision to detect and handle in migrated code, but there's undoubtedly more to it than I know.

@ghost ghost locked as resolved and limited conversation to collaborators May 25, 2022
@samhouts samhouts added the fixed-in-6.0.300-rc.2 Look for this fix in 6.0.300-rc.2! label Feb 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView fixed-in-6.0.300-rc.2 Look for this fix in 6.0.300-rc.2! platform/android 🤖 s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
6 participants