Forum Xamarin.Forms

Custom TimeControler doesnt work while add from code Behind on ios

MasterOfXamarinMasterOfXamarin Member ✭✭✭
edited March 24 in Xamarin.Forms

Hi, I am struggling with a stupid problem, I have made a custom TimePicker for Ios, it works perfectly when it is added in xaml

 <customControls:CustomTimePicker
                        x:Name="EndHour"
                        Grid.Row="9"
                        Grid.Column="1"
                        Focused="MyEndTimePicker_Focused"
                        Format="HH:mm"
                        HorizontalOptions="FillAndExpand"
                        PropertyChanged="ChangedEndHour"
                        PropertyChanging="OldChangedEndHour"
                        Style="{StaticResource MainTimeStyle}"
                        Time="{Binding EndHour}" />

but when I add this control form code behind

var leftTimePicker = new CustomTimePicker { HorizontalOptions = LayoutOptions.Center };
                var rightTimePicker = new CustomTimePicker { HorizontalOptions = LayoutOptions.Center };
                var rowToChange = MainGrid.RowDefinitions.ElementAt(RowNumber);

                var finalHeight = rowToChange.Height.Value + RowHeight;

                rowToChange = new RowDefinition { Height = new GridLength(finalHeight, GridUnitType.Star) };

                MainGrid.RowDefinitions[RowNumber] = rowToChange;

                var label = new Label { TextColor = Color.Blue };

                switch (dayPrior.Key)
                {
                    case "Mon":
                        label.Text = "Poniedziałek";
                        leftTimePicker.SetBinding(CustomTimePicker.TimeProperty, "MonStartHour");
                        rightTimePicker.SetBinding(CustomTimePicker.TimeProperty, "MonEndHour");
                        break;

This is my custom picker

 public class CustomTimePickerRenderer : TimePickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                var timePicker = (UIDatePicker)Control.InputView;
                timePicker.Locale = new NSLocale("no_nb");

                if (Element != null && !Element.Time.Equals(default(TimeSpan)))
                    Control.Text = Element.Time.ToString(@"hh\:mm");
                else
                    Control.Text = DateTime.Now.ToString("HH:mm");
            }
        }      
    }

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Custom TimeControler doesnt work while add from code Behind on ios

    Does not work tell us noting .

    What is the exact problem? Picker not show up ? Error happened ? Text not display as expected ?

Sign In or Register to comment.