Get the Datepicker(custom) value and save it to firebase in xamarin form

MSXAM 41 Reputation points
2022-12-01T13:49:28.453+00:00

Hello,Im beginner to xamarin form and Im working for our thesis. So my problem is I created a custom datepicker(for BIRTHDAY FIELD )( I got the source from internet). The reason why I created it because I want to put an placeholder in my UI. And now I will use that custom datepicker control to get the date(the birthday) I picked then save it to database(firebase).

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; }
}
//////////
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; }  
          
    }  

code in 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";  
            }  
        }  
    }  

Thank you for your help sir :)

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 45,181 Reputation points Microsoft Vendor
    2022-12-02T02:47:42.96+00:00

    Hello,

    It is more recommended to read and save the DatePicker via MVVM style, you could refer to the following documentation and video:

    Then, you could refer to the following specific code:

    Create your Model class, such as User:

       public class User  
       {  
           public DateTime BirthDay { get; set; }  
       }  
    

    Add user property and command to binding the value of datepicker for saving.

       public class AboutViewModel : BaseViewModel  
       {  
           public ICommand SaveToFireBaseCommand { get; }  
         
           private async Task SaveToFireBase()  
           {  
       	// Save User into FireBase.  
           }  
          private User _user;  
         
          public User User  
           {  
               get  
               {  
                   return _user;  
               }  
               set  
               {  
                   SetProperty(ref _user, value);  
               }  
           }  
           public AboutViewModel()  
           {  
               User = new User();  
       	SaveToFireBaseCommand = new Command(async () => await SaveToFireBase());  
           }  
       }  
    

    Finally, binding the properties on XAML.

       <c:BirthdayDatePickerControl Date="{Binding User.BirthDay}"/>  
       <Button Text="Submit" Command="{Binding SaveToFireBaseCommand}"/>  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.