question

zehguilherme avatar image
0 Votes"
zehguilherme asked Max-5836 answered

How can I get the value of a TimePicker?

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)");
            }
        }



dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Max-5836 avatar image
0 Votes"
Max-5836 answered

Rega hawlery as well as

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LandLu-MSFT avatar image
2 Votes"
LandLu-MSFT answered

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.






capture.png (2.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

jcmanke avatar image
1 Vote"
jcmanke answered jcmanke edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.