简体   繁体   中英

Clear Datepicker's value after clicking the submit button in xamarin form

My problem is, the data which is date entered by user is doesn't after submit button. So I have fields in my registration page and A button to save in my database. This is what I've tried. //My Datepicker Design `

<local:BirthdayDatePickerControl
                                                        TextColor="Black"
                                                      x:Name="entryField_DateOfBirth"
                                                      />

`

The purpose that I create a custom control in my datepicker is to put an placeholder iny my datepicker field. //my Birthdaypickercontrol.cs `

 public class BirthdayDatePickerControl : DatePicker
    {
        public event EventHandler ClearRequested;
        
        // for my placeholder "birthdate"
            public static readonly BindableProperty EnterTextProperty = BindableProperty.Create(propertyName: "Placeholder", returnType: typeof(string), declaringType: typeof(BirthdayDatePickerControl), defaultValue: default(string));
            public string Placeholder { get; set; }
        
        //function to clear data of my datepicker input
        public void clear()
        {
            if (ClearRequested != null)
            {
                ClearRequested(this, EventArgs.Empty);
            }
        }
    }

`

In my project.android, I create a birthday renderer.cs //so this is my code `

[assembly: ExportRenderer(typeof(BirthdayDatePickerControl), typeof(BirthdayDatePickerRenderer))]
  public class BirthdayDatePickerRenderer : DatePickerRenderer
    {
        public BirthdayDatePickerRenderer(Context context) : base(context)
        {

        }
        EditText editText;
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
        {
            base.OnElementChanged(e);
           //code for placeholder
            if (Control != null)
            {
                Control.Text = "Birth Date";
            }
            //end here

            //code  start here for clearing the data in datepicker input field
            editText = Control as EditText;
            if (e.NewElement != null)  
            {
                BirthdayDatePickerControl bdaydatePickerControl = e.NewElement as BirthdayDatePickerControl;
                 bdaydatePickerControl.ClearRequested += DatePickerControl_ClearRequested;
           
            }
            //end here
          
        }

        public void DatePickerControl_ClearRequested(object sender, EventArgs e)
        {
            editText.Text = string.Empty;
        }
    }

` The codes I pasted will anyway, but.. Assuming in the onload of my registration page, The UI will be like this( pic for reference and ctto to google) . After user choose birthdate, example 12/1/22 and hit submit button(all data save in to database). The problem is the placeholder "birthdate" remove/disappear Like this , then if I click the datepicker input field to check the date, the date is still pointing in 12/1/22. What I expected is after performing the ClearData(), the date should be reset in today's date.

//this is my ClearData() function `

public void ClearData()
        {
            entryField_DateOfBirth.clear();// this is what I tried and got an bad ouput


}

`

You said:

What I expected is after performing the ClearData(), the date should be reset in today's date.

        public void DatePickerControl_ClearRequested(object sender, EventArgs e)
        {
            editText.Text = string.Empty;
        }

Change to this:

        public void DatePickerControl_ClearRequested(object sender, EventArgs e)
        {
            editText.Text = DateTime.Now.ToShortDateString();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM