【问题标题】:Customizing the custom picker in Xamarin在 Xamarin 中自定义自定义选择器
【发布时间】:2022-01-02 15:19:40
【问题描述】:

我希望每个人都做得很好。如果我在屏幕上的项目上释放鼠标,有人可以帮助我如何删除“取消”和“确定”按钮并允许/引发事件选择,其次我需要帮助更改边框和背景的颜色颜色?感谢您的帮助,谢谢。

选择器:

    public class BorderlessPickerRenderer : PickerRenderer
{
    public static void Init() { }
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            Control.Background = Android.Graphics.Color.Rgb();
            string fontFamily = e.NewElement?.FontFamily;
            
            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);
        }
    }
}

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.android


    【解决方案1】:

    您可以使用 Custon Remderer 来定义自定义对话框。

    如果我在屏幕上的项目上释放鼠标,如何删除“取消”和“确定”按钮并允许/引发事件选择,

    AlertDialog 提供SetNegativeButton 事件来设置取消和确定按钮。在视图中删除两者就可以了。自定义视图是一个列表视图。您可以使用 ListView 的 ItemClick。

    其次,我在更改边框颜色和背景颜色方面需要帮助?

    您可以将对话框的背景颜色设置为更改。添加android:divider 设置边框颜色。并添加android:dividerHeight 设置边界高度。

    自定义渲染器:

     [assembly: ExportRenderer(typeof(Picker), typeof(BorderlessPickerRenderer))]
    namespace App15.Droid
    {
     public class BorderlessPickerRenderer : PickerRenderer
     {
        AlertDialog listDialog;
        string[] items;
        public BorderlessPickerRenderer(Context context) : base(context)
        {
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.Click += Control_Click1; ;
            }
        }
    
        private void Control_Click1(object sender, EventArgs e)
        {
            Picker model = Element;
            items = model.Items.ToArray();
            AlertDialog.Builder builder = new AlertDialog.Builder(this.Context);
            builder.SetTitle(model.Title ?? "");
            //builder.SetNegativeButton("Cancel", (s, a) =>
            //{
            //    Control?.ClearFocus();
            //    builder = null;
            //});
            Android.Views.View view = LayoutInflater.From(this.Context).Inflate(Resource.Layout.listview, null);
            Android.Widget.ListView listView = view.FindViewById<Android.Widget.ListView>(Resource.Id.listView1);
    
            MyAdapter myAdapter = new MyAdapter(items, Element.SelectedIndex);
            listView.Adapter = myAdapter;
            listView.ItemClick += ListView_ItemClick;
            builder.SetView(view);
            listDialog = builder.Create();
            listDialog.Window.DecorView.SetBackgroundColor(Android.Graphics.Color.Pink); // set the dialog background color 
            listDialog.Show();
            //Android.Widget.Button button = listDialog.GetButton((int)DialogButtonType.Negative);
            //button.Text = "Cancel";
            //button.Click += Button_Click;
            //button.SetTextColor(Android.Graphics.Color.Blue); // set the button bottom color
        }
        //private void Button_Click(object sender, EventArgs e)
        //{
        //    listDialog.Dismiss();
        //    listDialog = null;
        //}
        private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            Control.Text = items[e.Position];
            Element.SelectedIndex = e.Position;
            Console.WriteLine(items[e.Position]);
            listDialog.Dismiss();
            listDialog = null;
        }
        class MyAdapter : BaseAdapter
        {
            private string[] items;
            private int selectedIndex;
    
            public MyAdapter(string[] items)
            {
                this.items = items;
            }
    
            public MyAdapter(string[] items, int selectedIndex) : this(items)
            {
                this.selectedIndex = selectedIndex;
            }
    
            public override int Count => items.Length;
    
            public override Java.Lang.Object GetItem(int position)
            {
                return items[position];
            }
    
            public override long GetItemId(int position)
            {
                return position;
            }
    
            public override Android.Views.View GetView(int position, Android.Views.View convertView, ViewGroup parent)
            {
                if (convertView == null)
                {
                    convertView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.listview_item, null);
                }
                TextView textView = convertView.FindViewById<TextView>(Resource.Id.textView1);
                textView.Text = items[position];
                return convertView;
            }
        }
      }
     }
    

    Xaml:

        <Picker x:Name="picker"  Title="Title" VerticalOptions="CenterAndExpand" >
            <Picker.ItemsSource>
                <x:Array Type="{x:Type x:String}">
                    <x:String>Option 1</x:String>
                    <x:String>Option 2</x:String>
                    <x:String>Option 3</x:String>
                </x:Array>
            </Picker.ItemsSource>
        </Picker>
    

    listview.xml:您可以在 android 项目中创建 Resources/layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
           android:id="@+id/listView1"
           android:divider="@android:color/holo_green_light"
            android:dividerHeight="2dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>
    
    
    </LinearLayout>
    

    listview_item.xml :您可以在您的Resources/layout android 项目中创建。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
       <TextView
          android:id="@+id/textView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"/>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 2020-12-10
      • 2014-08-22
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多