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

Picker Attribute "SelectedIndex" Not being respected on page load on Android? #9150

Open
Programming-With-Chris opened this issue Aug 2, 2022 · 8 comments · May be fixed by #22353
Open
Labels
area-controls-picker Picker good first issue Good for newcomers platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@Programming-With-Chris
Copy link

Description

I have a Picker that is attached to an enum for a "list type" property, with a "SelectedIndex=0" attribute on the picker, which I believe means that I want that index of the enum to be selected first, as a 'default'. This works great on the windows app, but when running on Android, the picker control is empty. Then, I'm able to force something into the control by manually changing the "selected index" in the .xaml from say, 0 to 1, which I think then triggers the event, and updates in the app. But obviously since I think the point of SelectedIndex is to default something in, then, well, it should default something in.

I found a workaround for this by grabbing the picker control in the constructor in the code-behind for the page, and setting it's SelectedIndex property there, which fixes it. But I presume that the xaml property should work without needing to do that.

Here is the repo for the demo app i'm playing with -

Xaml for the view with the Picker - https://github.com/Programming-With-Chris/ShoppingList/blob/master/ShoppingList/View/UserListDataInput.xaml

Code Behind showing the 'work around' - https://github.com/Programming-With-Chris/ShoppingList/blob/master/ShoppingList/View/UserListDataInput.xaml.cs

Steps to Reproduce

  1. Use Picker Control in a ContentPage
  2. Set the "SelectedIndex="0"" value on the Picker
  3. See that on that page loading, nothing is populated on Android, but is on the Windows App

Version with bug

6.0.408 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

'android'">21.0

Did you find any workaround?

Yes, as described above - "I found a workaround for this by grabbing the picker control in the constructor in the code-behind for the page, and setting it's SelectedIndex property there, which fixes it. "

Relevant log output

No response

@Programming-With-Chris Programming-With-Chris added s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working labels Aug 2, 2022
@jfversluis jfversluis added good first issue Good for newcomers platform/android 🤖 legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor area-controls-picker Picker and removed s/needs-verification Indicates that this issue needs initial verification before further triage will happen labels Aug 2, 2022
@jfversluis jfversluis added this to the Backlog milestone Aug 2, 2022
@WdeBruin
Copy link
Contributor

WdeBruin commented Aug 17, 2022

Edit: Can confirm that this happens on iOS. Will check Windows and Mac tomorrow.

Investigated this for a few hours. It seems that in XamlNode.cs the properties of Picker have SelectedIndex above the Binding PickerItems.

image

It first then iterates into SelectedIndex, eventually it comes in BindableObject.cs in SetValueCore, where property.CoerceValue is executed against the selectedIndex from the xaml (1 in my example). It is coerced to -1 because there are no items yet, and after that it is not loaded from the XamlNode again.

The next property after SelectedIndex is the PickerItems Binding.

I have yet to figure out what determines the order in which properties are listed, so that I can change the behavior. As seen in the screenshot other properties x:name and textColor are applied after the Binding PickerItems.

@WdeBruin
Copy link
Contributor

I think this is the same issue as #7138

@WdeBruin
Copy link
Contributor

WdeBruin commented Aug 18, 2022

Reproduced the issue on Android, iOS, Windows and Mac Catalyst. Labels of issue can be updated. Also ideas about solution direction are welcome. @jfversluis

Edit: Also tested this in Xamarin Forms, and this issue is also reproducible there. Is this perhaps intended behavior or should an issue be created in Xamarin Forms github as well?

@richardlalancetteyoui
Copy link

Same issue here on Mac Catalyst.

@XamlTest XamlTest added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels May 25, 2023
@XamlTest
Copy link
Collaborator

Verified this on Visual Studio Enterprise 17.7.0 Preview 1.0. Repro on Windows 11, Android 13.0 and iOS 16.4 with below Project:
9150.zip

@MacrayBlackhand
Copy link

Frighteningly, more than a year now and nothing, yikes. In case anyone is still interested, this is still going on. In fact, I can't even set the SelectedIndex in the page's constructor as a work-around. The default value is -1 and even after an explicit setting of the value, it does not change.

@kramer-e
Copy link

kramer-e commented Dec 12, 2023

This is fixed in a preview of MAUI 8 #16387 Sadly, this is not backported to a service release of MAUI 7.

You're right, this still isn't fixed. The workaround is to use SelectedItem and convert it back to an index in your VM.

@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
kubaflo added a commit to kubaflo/maui that referenced this issue May 12, 2024
kubaflo added a commit to kubaflo/maui that referenced this issue May 12, 2024
@MartyIX
Copy link
Collaborator

MartyIX commented May 12, 2024

(Added labels per #9150 (comment) comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-picker Picker good first issue Good for newcomers platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants