Xamarin.forms 表单:从选择器的对话框中高亮显示所选项目

Xamarin.forms 表单:从选择器的对话框中高亮显示所选项目,xamarin.forms,Xamarin.forms,我有一个自定义选择器,它可以为对话框充气 但是,当您从列表中选取一个项目,然后再次单击选取器时,列表将重新出现,不会显示当前选取的项目 iOS实际上做到了这一点,但android没有 这是我的android选择器对话框: private void Control_Click(object sender, EventArgs e) { //throw new NotImplementedException(); Picker mod

我有一个自定义选择器,它可以为对话框充气

但是,当您从列表中选取一个项目,然后再次单击选取器时,列表将重新出现,不会显示当前选取的项目

iOS实际上做到了这一点,但android没有

这是我的android选择器对话框:

 private void Control_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            Picker model = Element;
            // Element


            string[] items = model.Items.ToArray();
            AlertDialog.Builder listDialog =
                new AlertDialog.Builder(context);
            listDialog.SetTitle(model.Title ?? "");

            listDialog.SetItems(items, (sender2, args) =>
            {
           
                ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, args.Which);
                if (Element != null)
                {
                    if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
                        Control.Text = model.Items[Element.SelectedIndex];
                    ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                    Control?.ClearFocus();
                }
                listDialog = null;
            });

            listDialog.SetNegativeButton("Zurück", (s, a) =>
            {
                ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
                Control?.ClearFocus();
                listDialog = null;
            });


            listDialog.Show();
        }

如何执行此操作?

您需要自定义
警报对话框
,您可以在其中添加
列表视图

请参阅以下代码(样式需要由您定义):

AndroidPickerRenderer课程:

public  class AndroidPickerRenderer : PickerRenderer
{
    private Context context;
    AlertDialog listDialog;
    string[] items;
    public AndroidPickerRenderer(Context context) : base(context)
    {
        this.context = context;
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
        if (Control !=null)
        {
            Control.Click += Control_Click;
        }
    }
   
    private void Control_Click(object sender, EventArgs e)
    {
            Picker model = Element;
            items = model.Items.ToArray();    
            AlertDialog.Builder builder =
           new AlertDialog.Builder(context);
            builder.SetTitle(model.Title ?? "");
            builder.SetNegativeButton("Zurück", (s, a) =>
            {
                Control?.ClearFocus();
                builder = null;
            });
            Android.Views.View view = LayoutInflater.From(context).Inflate(Resource.Layout.listview, null);
            ListView listView = view.FindViewById<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.Show();
    }

    private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        Control.Text = items[e.Position];
        Element.SelectedIndex = 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];
            if (position == selectedIndex)
            {
                textView.SetBackgroundColor(Color.Red.ToAndroid()); //highlight color
            }
            else
            {
                textView.SetBackgroundColor(Color.White.ToAndroid());//default color
            }
            return convertView;
        }
    }
}
公共类AndroidPickerRenderer:PickerRenderer
{
私人语境;
AlertDialog listDialog;
字符串[]项;
公共AndroidPickerRenderer(上下文):基(上下文)
{
this.context=上下文;
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
Control.Click+=Control\u Click;
}
}
私有无效控件\u单击(对象发送方,事件参数e)
{
选择器模型=元件;
items=model.items.ToArray();
AlertDialog.Builder=
新建AlertDialog.Builder(上下文);
builder.SetTitle(model.Title??);
builder.SetNegativeButton(“Zurück”,(s,a)=>
{
控件?.ClearFocus();
builder=null;
});
Android.Views.View=LayoutInflater.From(context).Inflate(Resource.Layout.listview,null);
ListView ListView=view.FindViewById(Resource.Id.listView1);
MyAdapter MyAdapter=新的MyAdapter(项,元素.SelectedIndex);
Adapter=myAdapter;
listView.ItemClick+=listView\u ItemClick;
builder.SetView(视图);
listDialog=builder.Create();
listDialog.Show();
}
私有无效列表视图\项目单击(对象发送者,AdapterView.ItemClickEventArgs e)
{
Control.Text=项目[e.Position];
Element.SelectedIndex=e.位置;
listDialog.disclose();
listDialog=null;
}
类MyAdapter:BaseAdapter
{
私有字符串[]项;
私有int-selectedIndex;
公共MyAdapter(字符串[]项)
{
这个项目=项目;
}
公共MyAdapter(字符串[]项,int-selectedIndex):此(项)
{
this.selectedIndex=selectedIndex;
}
public override int Count=>items.Length;
public override Java.Lang.Object GetItem(int位置)
{
退货项目[位置];
}
公共覆盖长GetItemId(int位置)
{
返回位置;
}
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_项,null);
}
TextView TextView=convertView.FindViewById(Resource.Id.textView1);
Text=items[位置];
如果(位置==所选索引)
{
textView.SetBackgroundColor(Color.Red.ToAndroid());//高亮显示颜色
}
其他的
{
textView.SetBackgroundColor(Color.White.ToAndroid());//默认颜色
}
返回视图;
}
}
}
listview.xml

<?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:layout_width="match_parent"
       android:layout_height="wrap_content"/>

 
</LinearLayout>

列表视图_item.xml

<?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>


您好,感谢您的努力:但是:我得到的错误是“textView1”不存在。我将这两个XML放在android项目的layout文件夹中(找到了它们),并且:但是,应用程序正在启动:但是没有返回列表视图项字符串[]项始终为零…不管第二部分是什么,这是我的错误^!非常感谢。