本文介绍了Xamarin.Forms Android DatePicker/TimePicker 按钮侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道用户是否点击了取消确定按钮在 xamarin.Forms Android 应用的日期/时间选择器中.

I need to know if a user tapped on the Cancel or Ok buttonin 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.Form 的 DatePicker,你不能这样做.Xamarin.Forms.DatePicker 呈现为 EditText 控件.不幸的是,弹出的 DatePickerDialog 没有暴露给用户.您可以从 Source 中看到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 按钮侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:21