简体   繁体   中英

Get the value of custom Datepicker and save it to firebase database

My problem is I want to insert datepicker's value to firebase but Idk how to do it.

I already made an custom date picker but the use of it is to make an placeholder "birth date" This is my xaml;

<frame //some property here>
<stacklayout  //some property here>
<frame  //some property here>
<image  //some property here/>
<grid>
<local:BirthdayDatePickerControl x:Name="entryField_DateOfBirth" >
                                    
                                </local:BirthdayDatePickerControl >
</grid>
</frame>
</stacklayout>
</frame>

This is my code(Myproj): Datepickerctrl.cs

public class DatePickerCtrl:DatePicker
{
public static readonly BindableProperty EnterTextProperty = BindableProperty.Create(propertyName: "Placeholder", returnType: typeof(string), declaringType: typeof(DatePickerCtrl), defaultValue: default(string));
public string Placeholder { get; set; }
}

this file or code also is save to (Myproj) BirthdayDatePickerControl.cs:

public class BirthdayDatePickerControl : DatePicker
     {
           
            
             public static readonly BindableProperty EnterTextProperty = BindableProperty.Create(propertyName: "Placeholder", returnType: typeof(string), declaringType: typeof(BirthdayDatePickerControl), defaultValue: default(string));
             public string Placeholder { get; set; }
            
     }

This is my code(Myproj.android):

[assembly: ExportRenderer(typeof(BirthdayDatePickerControl), typeof(BirthdayDatePickerRenderer))] // this line is outside of my namespace in BirthdayDatePickerRenderer.cs

BirthdayDatePickerRenderer.cs

public class BirthdayDatePickerRenderer : DatePickerRenderer
     {
         public BirthdayDatePickerRenderer(Context context) : base(context)
         {
    
         }
    
         protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.DatePicker> e)
         {
             base.OnElementChanged(e);
             if (Control != null)
             {
                 Control.Text = "Birth Date";
             }
         }
     }

My expected output is still I can place an placeholder to my datepicker UI, and also get that data from datepicker and same it to firebase.

I tried some trial and error This is what I've done

async public void signButton_Clicked(System.Object sender, System.EventArgs e)
        {

/*

some code here like assigning firstname or lastname and etc.
*/
  string dateOfBirth = entryField_DateOfBirth.ToString();

   CUSTOMER customer = new CUSTOMER();

   customer.CusBirthOfDate = dateOfBirth;
    var Savedata = await customerRepo.Save(customer);

         }

I think you have done most of the work. Just make a small change to the Button Clicked handler:

async public void signButton_Clicked(System.Object sender, System.EventArgs e)
{
    ... 
  
    var date = entryField_DateOfBirth.Date.ToString().Split(" ")[0]; //get the date
    
    ...
}

If you want to set the default placeholder of Datepicker to "Birth Date", you should still use a custom render, otherwise xamarin.forms Datepicker cannot recognize this kind of string. For more info, you could refer to Xamarin.Forms DatePicker .

Hope it works for you.

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