Xamarin 表单选择器确定或取消按钮按下检测
Posted
技术标签:
【中文标题】Xamarin 表单选择器确定或取消按钮按下检测【英文标题】:Xamarin Forms picker OK or Cancel button pressed detection 【发布时间】:2019-09-01 20:42:13 【问题描述】:我正在使用 Xamarin Forms 开发应用程序。我的选择器组件有一些问题。
用 Picker 拾取后,需要判断是按下 OK 还是 Cancel 按钮。
我尝试了 Focus 和 Unfocus,但它不能帮助我确定哪个用户正在按下 OK 或 Cancel 按钮。
【问题讨论】:
建议您制作一个自定义渲染器,然后创建在这些事件触发时通知您的本机处理程序 如何在这个主题上使用自定义渲染器?我需要将它用于 ios 和 android。我无法使用自定义渲染器捕获 OK 或 Cancel 按钮的点击事件。 您可以查看我的回答。如果我的回答对您有帮助,请不要忘记标记,以便更多人看到。 【参考方案1】:解决方案:
你应该在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 ;
安卓
[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 处理表单中的事件。
在内容页面中
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
);
【讨论】:
以上是关于Xamarin 表单选择器确定或取消按钮按下检测的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin.Forms Android DatePicker / TimePicker按钮侦听器