1

This is a common problem I have with events that are tied to UI controls. I want to do something with a particular event only if it was initiated directly by the user.

After reading Determine whether Selector.SelectionChanged event was initiated by a user, I realized that one may be able to determine that by examining the sending control. The control may have a property that differs depending on whether or not the user is currently interacting with it.

In this particular case, I am working with a Xamarin.Forms.Picker in a desktop PC app (UWP). I am wondering if there is a property that can tell me whether or not a selectionChanged event was initiated by the user. Surprisingly, the Focused property does NOT seem to be much help. I've seen user-initiated changes where the control is not Focused, and non-user-initiated changes where the control is Focused.

EDIT: I have now opened an issue on this: https://github.com/xamarin/Xamarin.Forms/issues/9128

William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • To my knowledge the only case `SelectedindexChanged` is called by system is when the control is rendered when the page is loaded. didn't this help you determine? – mshwf Jan 07 '20 at 21:55
  • I could certainly track when the page was loaded and ignore if within 1 second of that. I was hoping for something more bulletproof, but I don't know if it's possible. For example, someone might later add code that sets the selected index. – William Jockusch Jan 07 '20 at 22:01
  • 1
    Universal question. You can't know it was the user that changed the selection but you definitely know it was your code. Since you wrote it. So set a bool variable to true before, to false after. With a comment on the event handler so a maintainer knows what they forgot to do. – Hans Passant Jan 07 '20 at 23:16
  • @William, I want to use picker.IsFocused to check whether user initiate in SelectedindexChanged event, it works fine in Android, but have issue in uwp, so I suggest you can create new thread at github to check it is issue at Xamarin.uwp. – Cherry Bu - MSFT Jan 08 '20 at 07:13

0 Answers0