Xamarin表单选择器的“确定”或“取消”按钮按下检测

时间:2019-04-11 09:05:13

标签: c# xamarin xamarin.forms picker

我正在使用Xamarin Forms开发应用程序。我的选择器组件有问题。

在使用Picker进行拾取之后,我需要确定是否按下了OK或Cancel按钮。

我尝试了“聚焦”和“不聚焦”,但这并不能帮助我确定哪个用户正在按“确定”或“取消”按钮。

1 个答案:

答案 0 :(得分:2)

解决方案:

您应该在CustomRenderer中重写 Picker 的工具栏

  

iOS

[assembly:ExportRenderer(typeof(Picker),typeof(MyPickerRenderer))]
namespace App6.iOS
{
  public class MyPickerRenderer:PickerRenderer
  {
    public MyPickerRenderer()
    {

    }



    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);

        if(Control!=null)
        {



            UIPickerView pickerView = (UIPickerView)Control.InputView;


            // get the button Done and rewrite the event
            UIToolbar toolbar = (UIToolbar)Control.InputAccessoryView;

            UIBarButtonItem done = new UIBarButtonItem("OK", UIBarButtonItemStyle.Done, (object sender, EventArgs click) =>
            {

                MessagingCenter.Send<Object>(this,"Ok_Clicked");

            });

            UIBarButtonItem cancel = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, (object sender, EventArgs click) =>
            {

                MessagingCenter.Send<Object>(this, "Cancel_Clicked");
            });


            UIBarButtonItem empty = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);

            toolbar.Items = new UIBarButtonItem[] { cancel,empty, done };
        }

    }
  }
}
  

Android

[assembly: ExportRenderer(typeof(Picker), typeof(MyPickerRenderer))]
namespace App6.Droid
{
  public class MyPickerRenderer : PickerRenderer
  {
    IElementController ElementController => Element as IElementController;

    public MyPickerRenderer(Context context) : base(context)
    {

    }


    private AlertDialog _dialog;

    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement == null || e.OldElement != null)
            return;

        Control.Click += Control_Click;
    }

    protected override void Dispose(bool disposing)
    {
        Control.Click -= Control_Click;
        base.Dispose(disposing);
    }




    private void Control_Click(object sender, EventArgs e)
    {

        Picker model = Element;

        var picker = new NumberPicker(Context);
        if (model.Items != null && model.Items.Any())
        {

            picker.MaxValue = model.Items.Count - 1;
            picker.MinValue = 0;

            picker.SetDisplayedValues(model.Items.ToArray());

            picker.WrapSelectorWheel = false;
            picker.Value = model.SelectedIndex;
        }

        var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
        layout.AddView(picker);

        ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);

        var builder = new AlertDialog.Builder(Context);
        builder.SetView(layout);

        builder.SetTitle(model.Title ?? "");
        builder.SetNegativeButton("Cancel  ", (s, a) =>
        {
            MessagingCenter.Send<Object>(this, "Cancel_Clicked");
        });
        builder.SetPositiveButton("Ok ", (s, a) =>
        {
            ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
            // It is possible for the Content of the Page to be changed on SelectedIndexChanged.
            // In this case, the Element & Control will no longer exist.
            if (Element != null)
            {
                if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
                    Control.Text = model.Items[Element.SelectedIndex];
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                // It is also possible for the Content of the Page to be changed when Focus is changed.
                // In this case, we'll lose our Control.

            }
            MessagingCenter.Send<Object>(this, "Ok_Clicked");
        });

        _dialog = builder.Create();
        _dialog.DismissEvent += (ssender, args) =>
        {
            ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
        };
        _dialog.Show();
    }

  }
}

您可以使用MessagingCenter处理表单中的事件。

  

在contentPage

public MainPage()
{
   InitializeComponent();

   MessagingCenter.Subscribe<Object>(this, "Ok_Clicked", (sender)=> {

      picker.Unfocus();

      DisplayAlert("Title", "Ok has been clicked", "cancel");

      //do something you want

   });

   MessagingCenter.Subscribe<Object>(this, "Cancel_Clicked", (sender) => {

       picker.Unfocus();

       DisplayAlert("Title", "Cancel has been clicked", "cancel");

       //do something you want
     });

 }

enter image description here