enisn / UraniumUI

Uranium is a Free & Open-Source UI Kit for MAUI.
Apache License 2.0
983 stars 110 forks source link

Duplicate Execution of SelectedValueChangedCommand with Null Parameter on SelectedItem Update #629

Closed malsabi closed 1 month ago

malsabi commented 1 month ago

When integrating PickerField with both the SelectedItem property and the SelectedValueChangedCommand, an unexpected behavior occurs upon modifying the SelectedItem within the ViewModel. Specifically, the SelectedValueChangedCommand is triggered twice under these conditions:

  1. On the first invocation, the command is executed with the correct argument, which contains the actual, expected value.
  2. However, on the second invocation, the command is executed with an argument that unexpectedly is null.

This issue significantly impacts functionality, as it results in the SelectedItem being reset to null, thereby losing its originally assigned value. This duplication and subsequent nullification are causing numerous problems in scenarios where the preservation of the SelectedItem's value is critical.

enisn commented 1 month ago

I couldn't reproduce duplicate command calls, can you share a minimal reproduction example?

malsabi commented 1 month ago

After spending some more time observing the behavior, I've identified that the problem originates from the Picker control in MAUI. Specifically, if the SelectedItem property is set to a certain value, it unexpectedly resets to null.

This issue is similar to the one reported in dotnet/maui#14832. However, there's a key difference: in my case, the issue occurs within a ContentPage, not a Popup.

Given that the problem is not related to the SelectedValueChangedCommand, this issue can be considered irrelevant to the original concern and may be closed.

enisn commented 1 month ago

Yes, PickerField uses the default MAUI Picker inside InputField class, all the behaviors come from MAUI PickerField. I'll check the situation in the original issue and if there can be a workaround for now, I can share in here

malsabi commented 1 month ago

Actually It was a mistake from me, I was setting SelectedItem before making sure ItemsSource is initialized completely. Whenever the ItemsSource is initialized it will set the SelectedItem to null. Also this applies to CollectionView. A Quote from Microsoft documentation

A Picker can be initialized to display a specific item by setting the SelectedIndex or SelectedItem properties. However, these properties must be set after initializing the ItemsSource collection.