I want to fire a click event on the Done button on a Picker in Xamarin Forms. I found some people having custom render for entry, but how can we implement the done button in a Picker on Xamarin forms iOS?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use a custom renderer to achieve this. Looking at the source code for the Picker on iOS, you can see that the 'Done' button is added to a UIToolbar. You can get a reference to the button and then handle its 'Clicked' 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!!!!");
}
}