I am using an Xamarin.Forms.Picker control and handling the SelectedIndexChanged event.
The user scrolls thru the items in the picker control then the user selects the "Done" button that is prebuilt into the picker control. Is there anyway to capture when the user presses "Done" instead of when the index changes.
The index can change multiple times as the user scrolls thru the items and I don't want to handle unnecessary events as I only care about the last item selected.
Thanks in advance
<Picker x:Name="PickerMinStars" Title="Min number of 5 stars" SelectedIndexChanged="Handle_PickerStarsSelectedIndexChanged">
</Picker>
There are three workarounds to achieve your requirement.
set unfoucus
event on Picker , it triggers when the picker are going to dismiss (click on the Done button or the outside area of the picker).
delay the SelectedIndexChanged
event firing until after you tap Done button.
picker.On<iOS>().SetUpdateMode(UpdateMode.WhenFinished);
create Custom Renderers
to handle Done button click event.
public class MyPickerRenderer : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if(e.OldElement != null)
{
var toolbar = (UIToolbar)Control.InputAccessoryView;
var doneBtn = toolbar.Items[1];
doneBtn.Clicked -= DoneBtn_Clicked;
}
if(e.NewElement != null)
{
var toolbar = (UIToolbar)Control.InputAccessoryView;
var doneBtn = toolbar.Items[1];
doneBtn.Clicked += DoneBtn_Clicked;
}
}
void DoneBtn_Clicked(object sender, EventArgs e)
{
Console.WriteLine("Clicked!!!!");
}
}
Refer to
How can we handle the done button click event for a Xamarin Forms Picker?