问题
I have a xamarin picker with a list of items and I want to remove the picker when the "done" button is pressed on iPhone and "Oke" button on android.
I have the code to remove the picker. But i don't know what event_Handler that might by.
Code:
Picker picker = new Picker
{
Title = "What's in the slot?",
VerticalOptions = LayoutOptions.CenterAndExpand
//HorizontalOptions = LayoutOptions.Center
};
private void Displaypickerview(int row, int column)
{
if (status == "filling board")
{
foreach (string text in pickerText)
{
picker.Items.Add(text);
}
foreach (string ore in oreLevels)
{
picker.Items.Add(ore);
}
picker.SelectedIndexChanged += (sender, args) =>
{
if (picker.SelectedIndex == -1)
{
}
else
{
//change value of cell and button
Picker picker = (Picker)sender;
int index = picker.SelectedIndex;
if (index < pickerText.Length)
{
board[row, column].Text = pickerText[index - 1];
}
else {
board[row, column].Text = oreLevels[index - 1 - pickerText.Length];
}
}
};
}
else if (status == "choosing item")
{
}
}
Example of what it looks like on iPhone:
回答1:
Unfortunately, you cannot use the same event for Android and iOS in your case, because:
Currently the Picker control sends the SelectedIndexChanged event on Android after the OK button is pressed. However, on iOS the event is sent whenever the user scrolls the picker and lets it stop on an item
It's known issue, I take quote from here.
You should combine SelectedIndexChanged and Unfocus events to achieve your goal. You can find some solutions here in this topic https://forums.xamarin.com/discussion/20847/picker-selection-event
UPD: Looks like I didn't understand your question right. If I did it right now, so you have to use custom renderers and specify needed logic in them.
For iOS. Create custom renderer inherited of PickerRenderer and implement something like that:
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
var extendedPicker = e.NewElement as ExtendedPicker;
if (extendedPicker == null) return;
var toolbar = new UIToolbar(new CGRect(0.0f, 0.0f, Control.Frame.Size.Width, 44.0f));
toolbar.Items = new[]
{
new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace),
new UIBarButtonItem("Done",
UIBarButtonItemStyle.Done,
delegate {
Control.ResignFirstResponder();
})
};
if (this.Control != null)
{
Control.InputAccessoryView = toolbar;
}
}
For Andorid looks like it works from the box
回答2:
There is now a platform specific configuration option that lets you enable this on iOS.
You specify a PickerMode
on a specific picker to only select once someone hits done on iOS.
<ContentPage ...
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core">
<StackLayout Margin="20">
<Picker ... Title="Select a monkey" ios:Picker.UpdateMode="WhenFinished">
...
</Picker>
...
</StackLayout>
</ContentPage>
The Picker.On<iOS>
method specifies that this platform-specific will only run on iOS. The Picker.SetUpdateMode method, in the Xamarin.Forms.PlatformConfiguration.iOSSpecific
namespace, is used to control when item selection occurs, with the UpdateMode enumeration providing two possible values:
Immediately
– item selection occurs as the user browses items in the Picker. This is the default behavior in Xamarin.Forms.
WhenFinished
– item selection only occurs once the user has pressed the Done button in the Picker.
Read docs for more info on control. https://docs.microsoft.com/sr-latn-rs/xamarin/xamarin-forms/platform/ios/picker-selection
来源:https://stackoverflow.com/questions/42193431/event-handler-of-the-done-button-of-a-picker