选择器的高度 Xamarin.Forms

Height of Picker in Xamarin.Forms

我正在使用 xamarin.forms 并在屏幕上显示可绑定选择器。

我正在使用以下代码来更新选择器控件的初始文本。

        public class MyCustomBindablePickerRenderer :PickerRenderer
        {
            // Override the OnElementChanged method so we can tweak this renderer post-initial setup
            protected override void OnElementChanged (ElementChangedEventArgs<Picker> e)
            {
                base.OnElementChanged (e);

                //Background = A Android.Graphics.Color.Rgb (0, 0, 0);
                if (Control != null) { 
                    // do whatever you want to the textField here!

                    Control.SetBackgroundColor(global::Android.Graphics.Color.Rgb(241,241,241));
                    //Control.ShadowRadius = 2;
                    //Control.SetCursorVisible = true;


                    var label = (TextView)Control; // for example
                    Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "avenir-book.ttf");
                    label.Typeface = font;
                    label.TextSize = CustomFonts.EntryFontSize;
                    label.SetHintTextColor (global::Android.Graphics.Color.Rgb(162,162,162));
                    label.SetTextColor (global::Android.Graphics.Color.Rgb(162,162,162));


                }
            }
        }

现在,我想增加单击文本时出现的弹出窗口的大小。

我用谷歌搜索并找到了事件 OnWindowFocusChanged,但是事件在弹出窗口出现后触发,所以我无法访问弹出窗口的高度。

任何人都可以建议当我点击选择器控件的文本框时触发事件吗?

如何更改弹出窗口的高度?

您需要创建一个 SpinnerAdapter,以便您可以自定义每个单元格。重写 GetDropDownView 方法,如下所示:

[assembly: ExportRenderer(typeof(UIPicker), typeof(UIPickerRenderer))]
namespace SampleApp.Android
{
    public class UIPickerRenderer: ViewRenderer<UIPicker, Spinner>
    {
        bool initialized = false;
        Spinner picker;
        static int selectedIndex=-1;
        protected override void OnElementChanged (ElementChangedEventArgs<UIPicker> e)
        {

            base.OnElementChanged (e);
            if(e.OldElement == null)
            {
                Element.SelectedIndexChanged += NotifyVM;

                selectedIndex = Element.SelectedIndex;
                this.picker = new Spinner(Forms.Context, SpinnerMode.Dropdown);
                var adapter = new SpinnerAdapter(Forms.Context, Android.Resource.Layout.SimpleSpinnerItem,Element.Items);

                picker.Adapter = adapter;
                picker.SetSelection(selectedIndex);
                picker.ItemSelected += picker_ItemSelected;
                this.SetNativeControl(picker);
                Element.SelectedIndexChanged += (object s, EventArgs args) => { 
                    if (picker.SelectedItemPosition != Element.SelectedIndex) 
                        picker.SetSelection(Element.SelectedIndex); 
                };
            }
        }

        private void picker_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            if(! initialized)
            {
                initialized = true;
                return;
            }
            Element.SelectedIndex = picker.SelectedItemPosition;
            var g = picker.SelectedItemPosition;
        }

        private async void NotifyVM(object sender, EventArgs e)
        {

            if (selectedIndex == Element.SelectedIndex)
                return;
            selectedIndex = Element.SelectedIndex;


            if (Element.ParentView.BindingContext != null && Element.IsVisible && Element.ShouldUpdateServices)
            {
               //Do your binding things
            }
        }

        internal class SpinnerAdapter : ArrayAdapter<string>
        {
            public SpinnerAdapter(Android.Content.Context context, int id, System.Collections.Generic.IList<string> items)
                : base(context, id, items)
            {
            }

            public override Android.Views.View GetDropDownView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
            {
                convertView = base.GetDropDownView(position, convertView, parent);

                convertView.Visibility = Android.Views.ViewStates.Visible;
                FormsViewGroup.LayoutParams p = convertView.LayoutParameters;
                p.Height = 60; //adjusting cell height
                p.Width = 300; //Adjusting cell width
                convertView.LayoutParameters = p;
                return convertView;
            }
        }

    }
}