xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] Android TimePickerRenderer dialog positive button handler not triggered #13022

Open janduldhardt opened 3 years ago

janduldhardt commented 3 years ago

Description

When implementing a custom renderer for the TimePicker in Android to intercept whether the dialogs OK button is pressed, the handler is not triggered for the DialogButtonType.Positive. Negative and Neutral button handlers are triggered.

The same code is working for DatePickerRenderer

Steps to Reproduce

    protected override TimePickerDialog CreateTimePickerDialog(int hours, int minutes)
    {
        dialog = new TimePickerDialog(Context, this, hours, minutes, true);

        dialog.SetButton((int) DialogButtonType.Negative,
            Context.Resources.GetString(global::Android.Resource.String.Cancel), OnCancel);
        dialog.SetButton((int) DialogButtonType.Positive,
            Context.Resources.GetString(global::Android.Resource.String.Ok), OnOk);

        return dialog;
    }

    private void OnCancel(object sender, DialogClickEventArgs e)
    {
        _element.Unfocus();
    }

    private void OnOk(object sender, EventArgs eventArgs)
    {
        _element.Unfocus();
    }

Expected Behavior

When clicking the OK button OnOk is called. When clicking the Cancel button OnCancel is called.

Actual Behavior

When clicking the OK button OnOk is NOT called. When clicking the Cancel button OnCancel is called.

Basic Information

Workaround

Instead I need to use the following code in my control:

this.PropertyChanged += (sender, args) =>
            {
                if (Device.RuntimePlatform != Device.Android || args.PropertyName != "Time")
                {
                    return;
                }

                CallOnOk();
            };
jgoyvaerts commented 3 years ago

This seems to be a "bug" in Android. See more info at https://stackoverflow.com/questions/52817245/timepickerdialog-onclick-on-button-positive-stopped-working-post-oreo , in the comments below the question someone mentions the cause and a workaround.