Picker的完成按钮单击事件

Picker的完成按钮单击事件

本文介绍了我们如何处理Xamarin Forms Picker的完成按钮单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Xamarin Forms中的选择器上的完成按钮上触发点击事件。我发现有些人具有用于输入的自定义渲染,但是如何在Xamarin Forms iOS的Picker中实现完成按钮?

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?

推荐答案

您可以使用自定义渲染器来实现此目的。查看(对于iOS上的Picker),您可以看到完成按钮已添加到UIToolbar。您可以获取对该按钮的引用,然后处理其单击事件:

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!!!!");
    }
}

这篇关于我们如何处理Xamarin Forms Picker的完成按钮单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:42