xamarin 表单自定义呈现的选择器

xamarin forms custom rendered picker

我创建了一个自定义选择器,以便拥有一个具有厚度框架、清除按钮和箭头按钮的控件

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

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

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

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

    public static readonly BindableProperty ImageProperty =
    BindableProperty.Create(nameof(Image), typeof(string), typeof(BorderlessPicker), string.Empty);

    public string Image
    {
        get { return (string)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }
    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);
    }

    public BorderlessPicker()
    {
        this.BackgroundColor = Color.Transparent;
    }

   

}

用于清除背景的自定义渲染器:

  public class BorderlessPickerRenderer : PickerRenderer
{
    public BorderlessPickerRenderer(Context context) : base(context)
    {
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {            
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            Control.Background = null;
        }
    }

}

当我打开选择器时,我得到一个“半屏”列表,其中大部分项目对用户都是隐藏的。 是这样的:

我想要的是当我打开选择器时我得到默认的“全屏”:

可能吗?

一切皆有可能,但定制的东西意味着更多的工作。

在Android上Picker是基于Android的Picker控制,在iOS上是UIPickerView的组合和 UITextField 链接在一起。

因此,如果您想更改当前 Picker,它会受到那些底层控件的限制。而你所要求的从那方面来说是不可能的。

但是,没有人会阻止您使用一个按钮(或其他控件)来打开全屏弹出窗口,您可以在其中选择值。

您将不得不使用 AppCompact PickerRenderer

public class BorderlessPickerRenderer : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
    {
        public BorderlessPickerRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
            Control.Background = null;
        }

    }