How can I get the value of a TimePicker?

2020-11-18T20:03:02.447+00:00

This screen consists of a form that will store the days of the week and their respective times when a sports court is open to the public.

The HoraInicio (initial time) and HoraFim (end time) properties are coming with the value 00: 00: 00.

1732s.png
jGCqe.png

XAML

xaml
<StackLayout BindableLayout.ItemsSource="{Binding Horarios}">
                        <BindableLayout.ItemTemplate>
                            <DataTemplate>
                                <StackLayout Orientation="Horizontal" HeightRequest="40">
                                    <Switch x:Name="AbertoSwitch"
                                            IsToggled="{Binding Aberto}"
                                            Toggled="AlteracaoDoEstadoDoSwitch"
                                            VerticalOptions="Center"/>
                                    <Label Text="{Binding DiaDaSemanaText}"
                                           HorizontalOptions="FillAndExpand"
                                           VerticalOptions="Center"/>
                                    <TimePicker BindingContext="{x:Reference AbertoSwitch}"
                                                IsVisible="{Binding IsToggled}"
                                                VerticalOptions="Center"
                                                Format="HH:mm"
                                                Time="{Binding HoraInicio}"/>
                                    <TimePicker BindingContext="{x:Reference AbertoSwitch}"
                                                IsVisible="{Binding IsToggled}"
                                                VerticalOptions="Center"
                                                Format="HH:mm"
                                                Time="{Binding HoraFim}"/>
                                </StackLayout>
                            </DataTemplate>
                        </BindableLayout.ItemTemplate>
``</StackLayout>

ViewModel

public TimeSpan HoraInicio { get => horaInicio; set => SetProperty(ref horaInicio, value); }
public TimeSpan HoraFim { get => horaFim; set => SetProperty(ref horaFim, value); }

public void AtribuirHorarioParaCadaDiaDisponivel()
        {
            try
            {
                for (int i = 0; i <= 6; i++)
                {
                    if (this.Horarios[i].Aberto == true)
                    {
                        this.Horarios[i].HoraInicio = this.HoraInicio.ToString();
                        this.Horarios[i].HoraFim = this.HoraFim.ToString();
                    }
                }
            }
            catch (Exception)
            {
                throw new Exception("Horário(s) incorreto(s)");
            }
        }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. LandLu-MSFT 21 Reputation points
    2020-11-19T03:45:54.137+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Do not change the binding context. We could use source to complete the binding:

    <TimePicker IsVisible="{Binding IsToggled, Source={x:Reference HorarioSwitch}}"  
                VerticalOptions="Center"  
                Format="HH:mm"  
                Time="{Binding BindingContext.HoraInicio, Source={x:Reference RootLayout}}"/>  
    

    So does another time picker:

    <TimePicker IsVisible="{Binding IsToggled, Source={x:Reference HorarioSwitch}}"  
                VerticalOptions="Center"  
                Format="HH:mm"  
                Time="{Binding BindingContext.HoraFim, Source={x:Reference RootLayout}}"/>  
    

    RootLayout here is your StackLayout:
    40918-capture.png

    Name it as you want.

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    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.

    2 people found this answer helpful.
    0 comments No comments

  2. Joe Manke 1,091 Reputation points
    2020-11-18T20:46:28.933+00:00

    You're binding the TimePicker to the Switch. Switch does not have HoraInicio or HoraFim properties.

    Instead of this:

    <TimePicker BindingContext="{x:Reference AbertoSwitch}" IsVisible="{Binding IsToggled}">
    

    Just bind TimePicker.IsVisible to Aberto the same way you're binding Switch.IsToggled.

    1 person found this answer helpful.
    0 comments No comments

  3. Max 1 Reputation point
    2021-05-13T12:30:21.083+00:00

    Rega hawlery as well as

    0 comments No comments