如何自定义时间间隔Xamarin表单?

时间:2018-02-08 12:35:12

标签: xamarin.forms

我正在使用Xamarin Froms PCL项目。我想在Android和IOS平台上以30分钟的间隔自定义时间选择器。

怎么可能。我正在使用Android的自定义代码:

public class BorderlessTimePickerRenderer : TimePickerRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            Control.SetBackground(Android.Support.V4.Content.ContextCompat.GetDrawable(this.Context, Android.Resource.Color.Transparent));
            var layoutParams = new MarginLayoutParams(Control.LayoutParameters);
            layoutParams.SetMargins(0, 0, 0, 0);
            LayoutParameters = layoutParams;
            Control.LayoutParameters = layoutParams;
            Control.SetPadding(0, 0, 0, 0);
            SetPadding(0, 0, 0, 0);
        }
    }
}

对于IOS:

public class BorderlessTimePickerRenderer : TimePickerRenderer
{
    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        Control.Layer.BorderWidth = 0;
        Control.BorderStyle = UITextBorderStyle.None;

    }
}

1 个答案:

答案 0 :(得分:1)

以下显示了自定义渲染器中iOS的实现:

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

        Control.Layer.BorderWidth = 0;
        Control.BorderStyle = UITextBorderStyle.None;


        var timePicker = (UIDatePicker)Control.InputView;
        timePicker.MinuteInterval = 30;
    }
}

正如您所见,我们将InputView的{​​{1}}投放到Control,而我们可以访问该属性UIDatePicker