Xamarin.Forms Android DatePicker/TimePicker按钮侦听器 [英] Xamarin.Forms Android DatePicker/TimePicker button listener
问题描述
我需要知道用户是否点击了取消或确定按钮
在xamarin.Forms
Android应用的日期/时间选择器中.
I need to know if a user tapped on the Cancel or Ok button
in a Date/Time picker of an xamarin.Forms
Android app.
我试图向Click事件中添加一个函数,但这从未触发.
I have tried to add a function to the Click event but this is never triggered.
这是我的代码(与TimePicker相似):
This is my code (similar for the TimePicker):
using Android.Content;
using MyApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using MyApp.Models;
using Java.Util;
[assembly: ExportRenderer(typeof(DatePicker), typeof(DatePickerCustomRenderer))]
namespace MyApp.Droid {
public class DatePickerCustomRenderer : DatePickerRenderer {
public DatePickerCustomRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e) {
base.OnElementChanged(e);
Locale locale = new Locale(Utente.FormatoPerDateTimePicker);
Control.TextLocale = locale;
Control.Click += Control_Click;
}
private void Control_Click(object sender, System.EventArgs e) {
// This function is never called :(
throw new System.NotImplementedException();
}
}
}
我该怎么做?
推荐答案
我需要知道用户是否点击了xamarin.Forms Android应用程序的日期/时间选择器中的取消"或确定"按钮.
I need to know if a user tapped on the "cancel" or on the "ok" button in a date/time picker of an xamarin.Forms Android app.
使用Xamarin.Form的DatePicker
,您将无法做到这一点. Xamarin.Forms.DatePicker
呈现到EditText
控件中.不幸的是,弹出窗口DatePickerDialog
并未对用户公开.您可以从来源中看到DatePickerRenderer的代码.
With Xamarin.Form's DatePicker
, you can't do that. Xamarin.Forms.DatePicker
is renderered into an EditText
control. The pop up DatePickerDialog
is unfortunately not exposed to user. You can see that from Source Codes of DatePickerRenderer.
因此,如果您确实要检测确定/取消"按钮的单击事件.您需要根据源代码实现自己的DatePickerRenderer.
So if you really want to detect the OK/Cancel button click events. You need to implement your own DatePickerRenderer according to the source codes.
这篇关于Xamarin.Forms Android DatePicker/TimePicker按钮侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!