微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为 DatePicker 创建自定义渲染器圆角、背景颜色等

如何解决如何为 DatePicker 创建自定义渲染器圆角、背景颜色等

因此,我首先为 Entry 提供了一个很棒的解决方案,并且能够轻松自定义 xaml 中的属性。但是现在我想要一个 DatePicker 的解决方案,我已经尝试以同样的方式来做,但我无法让它工作。

我主要希望能够改变背景颜色、边框、圆角边框并添加一些填充。但是使用我尝试的解决方案,我只能在您选择日期时更改颜色..

如果有人可以提供帮助并可能解释如何在 IOS 上做到这一点,那就太棒了。 非常感谢。

enter image description here

但我真正想要的是 DatePicker 看起来像绿色 Entry

enter image description here

这是我为参赛作品所做的:

TextEntry.cs:

public class TextEntry : Entry
    {
        public static BindableProperty CornerRadiusProperty =
            BindableProperty.Create(nameof(CornerRadius),typeof(int),typeof(TextEntry),0);

        public static BindableProperty BorderThicknessProperty =
            BindableProperty.Create(nameof(BorderThickness),0);

        public static BindableProperty PaddingProperty =
            BindableProperty.Create(nameof(Padding),typeof(Thickness),new Thickness(5));

        public static BindableProperty BorderColorProperty =
            BindableProperty.Create(nameof(BorderColor),typeof(Color),Color.Transparent);



        public int CornerRadius
        {
            get => (int)GetValue(CornerRadiusProperty);
            set => SetValue(CornerRadiusProperty,value);
        }

        public int BorderThickness
        {
            get => (int)GetValue(BorderThicknessProperty);
            set => SetValue(BorderThicknessProperty,value);
        }
        public Color BorderColor
        {
            get => (Color)GetValue(BorderColorProperty);
            set => SetValue(BorderColorProperty,value);
        }
        /// <summary>
        /// This property cannot be changed at runtime in iOS.
        /// </summary>
        public Thickness Padding
        {
            get => (Thickness)GetValue(PaddingProperty);
            set => SetValue(PaddingProperty,value);
        }
    }

TextEntryRendererAndroid.cs

[assembly: ExportRenderer(typeof(TextEntry),typeof(TextEntryRendererAndroid))]
namespace maPaye.Droid
{
    public class TextEntryRendererAndroid : EntryRenderer
    {
        public TextEntryRendererAndroid(Context context): base(context)
        {

        }

        public TextEntry ElementV2 => Element as TextEntry;
        protected override FormsEditText CreateNativeControl()
        {
            var control = base.CreateNativeControl();
            UpdateBackground(control);
            return control;
        }
        protected override void OnElementPropertyChanged(object sender,PropertyChangedEventArgs e)
        {
            if (e.PropertyName == TextEntry.CornerRadiusProperty.PropertyName)
            {
                UpdateBackground();
            }
            else if (e.PropertyName == TextEntry.BorderThicknessProperty.PropertyName)
            {
                UpdateBackground();
            }
            else if (e.PropertyName == TextEntry.BorderColorProperty.PropertyName)
            {
                UpdateBackground();
            }

            base.OnElementPropertyChanged(sender,e);
        }

        protected override void UpdateBackgroundColor()
        {
            UpdateBackground();
        }
        protected void UpdateBackground(FormsEditText control)
        {
            if (control == null) return;

            var gd = new GradientDrawable();
            gd.SetColor(Element.BackgroundColor.ToAndroid());
            gd.SetCornerRadius(Context.ToPixels(ElementV2.CornerRadius));
            gd.Setstroke((int)Context.ToPixels(ElementV2.BorderThickness),ElementV2.BorderColor.ToAndroid());
            control.SetBackground(gd);

            var padTop = (int)Context.ToPixels(ElementV2.Padding.Top);
            var padBottom = (int)Context.ToPixels(ElementV2.Padding.Bottom);
            var padLeft = (int)Context.ToPixels(ElementV2.Padding.Left);
            var padright = (int)Context.ToPixels(ElementV2.Padding.Right);

            control.SetPadding(padLeft,padTop,padright,padBottom);
        }
        protected void UpdateBackground()
        {
            UpdateBackground(Control);
        }
}
}

现在我尝试为 DatePicker 重现它,但无法将边框/角半径/背景颜色添加到您选择日期的输入框

DatePickerEntry.cs

   public class DatePickerEntry : DatePicker
    {
        public static BindableProperty CornerRadiusProperty =
            BindableProperty.Create(nameof(CornerRadius),typeof(DatePicker),value);
        }

        public Thickness Padding
        {
            get => (Thickness)GetValue(PaddingProperty);
            set => SetValue(PaddingProperty,value);
        }
    }

DatePickerRendererAndroid.cs

[assembly: ExportRenderer(typeof(DatePickerEntry),typeof(DatePickerRendererAndroid))]
namespace maPaye.Droid
{
    public class DatePickerRendererAndroid : DatePickerRenderer
    {

        public DatePickerRendererAndroid(Context context) : base(context)
        {

        }
        public DatePickerEntry ElementV2 => Element as DatePickerEntry;
        protected override EditText CreateNativeControl()
        {
            var control = base.CreateNativeControl();
            UpdateBackground(control);
            return control;
        }

        protected override void OnElementPropertyChanged(object sender,PropertyChangedEventArgs e)
        {
            if (e.PropertyName == DatePickerEntry.CornerRadiusProperty.PropertyName)
            {
                UpdateBackground();
            }
            else if (e.PropertyName == DatePickerEntry.BorderThicknessProperty.PropertyName)
            {
                UpdateBackground();
            }
            else if (e.PropertyName == DatePickerEntry.BorderColorProperty.PropertyName)
            {
                UpdateBackground();
            }

            base.OnElementPropertyChanged(sender,e);
        }

        protected void UpdateBackground(EditText control)
        {
            if (control == null) return;

            var gd = new GradientDrawable();
            gd.SetColor(Element.BackgroundColor.ToAndroid());
            gd.SetCornerRadius(Context.ToPixels(ElementV2.CornerRadius));
            gd.Setstroke((int)Context.ToPixels(ElementV2.BorderThickness),padBottom);
        }
        protected void UpdateBackground()
        {
            UpdateBackground(Control);
        }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。