如何为 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 (将#修改为@)