dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.99k stars 1.72k forks source link

DatePicker doesnt fire for DateSelected event on same date #13156

Open duindain opened 1 year ago

duindain commented 1 year ago

Description

The DatePicker has a default date of todays date, if someone selects that same date the dialog closes but the DateSelected doesn't fire

If you didn't pass in an initial date this effectively prevents a user from selecting today's date, if you do have a date bound it prevents selecting that date which means any extra code in the DateSelected event doesn't fire

This is related to https://github.com/dotnet/maui/issues/9180 though that was Android specific and for .Net 6

Steps to Reproduce

  1. Create File > New .Net Maui App
  2. Add a DatePicker to MainPage.xaml
  3. Add a DateSelected event handler
  4. Add a debug statement to the handler to see when the event is fired
  5. Run the app and select todays date, no event is fired

Link to public reproduction project repository

https://github.com/duindain/DateChanger

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, I was not able test on other platforms

Affected platform versions

Android 11 and up, WindowsSDK 10.0.19041, iOS 16.1

Did you find any workaround?

No response

Relevant log output

No response

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

mattleibow commented 1 year ago

Maybe this issue is more we need a "null date" option so that there is a change. Or maybe we need a DateChanged and PickerClosed event?

I see this PR does something: https://github.com/dotnet/maui/pull/9727/files?w=1

However it changes the property type and I think that is a bit breaking. We could use things like DateTime.MinDate instead...

duindain commented 1 year ago

A nullable date would be ideal, i actually discovered this while creating my own nullable datepicker

The event is DateSelected not DateChanged it should trigger on any date selected not just changes

hariywill commented 1 year ago

How can you say MAUI is an upgrade of Xamarin, when the most basic stuff is not even fixed?

PureWeen commented 9 months ago

Archive.zip

Here's a workaround that should work on ios/android/winui

Basically the date is always passed back up into the xplat layer but the BP doesn't fire a property changed so you just need to intercept via the interface.

I'm not really sure how to make this work on catalyst though. On catalyst the event only fires when the value is changed and I couldn't figure out a way to detect when the dialog is opened and then closed. I think on catalyst you might have to get clever with it, like, have two controls on the screen and the non visible one opens the dialog.

Unless someone knows of a way to make it work?

AFAIK

In Mac Catalyst, there isn't a direct way to know if the UIDatePicker dialog has been opened or closed because UIDatePicker does not provide events for these actions. However, you can use the ValueChanged event to know when a user has interacted with the UIDatePicker.

ValueChanged doesn't fire though when the Value hasn't changed.

using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;

namespace DateChanger
{
    public class CustomDatePicker : DatePicker, IDatePicker
    {
        public CustomDatePicker()
        {
        }

        protected override void OnHandlerChanging(HandlerChangingEventArgs args)
        {
            base.OnHandlerChanging(args);

#if WINDOWS
            if (args.OldHandler is IDatePickerHandler dph)
            {
                dph.PlatformView.Closed -= PlatformView_Closed;
            }
#endif

        }

        protected override void OnHandlerChanged()
        {
            base.OnHandlerChanged();

            if (Handler is IDatePickerHandler dphNew)
            {
#if WINDOWS
                dphNew.PlatformView.Closed += PlatformView_Closed;
#endif
            }
        }

#if WINDOWS
        private void PlatformView_Closed(object sender, object e)
        {
            OnPropertyChanged(nameof(Date));
        }
#endif

        DateTime IDatePicker.Date 
        { 
            get => Date;
            set
            {
                Date = value;
                OnPropertyChanged(nameof(Date));
            }
        }
    }
}
its-AliRaza commented 7 months ago

Here's a workaround that should work.


 public class FormDatePicker : Microsoft.Maui.Controls.DatePicker, IDatePicker
 {
     DateTime IDatePicker.Date
     {
         get => Date;
         set
         {
             if (value.Equals(DateTime.Today.Date))
                 Date = value.AddDays(-1);
             Date = value;
             OnPropertyChanged(nameof(Date));
         }
     }
 }
corne-ac commented 3 months ago

This is still an issue, and the same for the TimePicker. its been over a year and still no fix. Used the Workaround suggested by @its-AliRaza .