0

I'm currently trying to receive the DateTime from a Xamarin.Forms.TimePicker. I just want to populate the model which is a DateTime pickerTime variable. This variable will be populated by whatever is in the TimePicker view.

Therefore I need to do OneWayToSource data binding but I only ever find examples in XAML, where as I'm working in C# file because its an Android custom render of a abstract Xamarin.Forms Page.

Is there any way to OneWayToSource data bind in C#? If not I would try making the Forms View page type of my abstract sub-classed page so I have access to a XAML page and make the TimePicker view as well the data bound object in there that the Custom Renderer Pages inherit with OneWayToSource data binding.

Thanks for your help!

TaiT's
  • 3,138
  • 3
  • 15
  • 26
B Best
  • 1,106
  • 1
  • 8
  • 26

1 Answers1

0

Of course you can. Considering you have already set the BindingContext, this XAML:

<TimePicker x:Name="myTimePicker" Time="{Binding Property, Mode=OneWayToSource}"/>

is the same as this in C#:

TimePicker myTimePicker = new TimePicker();
myTimePicker.SetBinding(TimePicker.TimeProperty, new Binding("Property") { Mode = BindingMode.OneWayToSource });

FYI, anything you can do in XAML can be done using C#!

TaiT's
  • 3,138
  • 3
  • 15
  • 26
  • Looking online it seems that the TimeProperty is of type `TimeSpan`. Is there a way to get this to `DateTime` type? – B Best Jun 05 '17 at 11:45
  • Or rather will using the Ticks operation on the `TimeSpan` give me the time in milliseconds as a `long` since that is what I need in the end. – B Best Jun 05 '17 at 11:52
  • @BBest If you need the date and time, just go for a `DateTimePicker` control. It's easy to adapt this piece of code with your suitable control. – TaiT's Jun 05 '17 at 13:08
  • It's for gettin the time for an alarm so is it even necessary to get the date? – B Best Jun 06 '17 at 14:38
  • It really depends on your business needs. I.e. If a future implementation needs the ability to set up an alarm the following day/week/month instead of the current day. It's up to you! – TaiT's Jun 06 '17 at 15:18
  • Ahh good point, probably useful for repeating alarms on certain days – B Best Jun 06 '17 at 17:35