自定义Xamarin.Forms选择器弹出列表
我知道如何创建自定义渲染器来自定义Xamarin forms picker的实际文本,但如何自定义(比如)单击picker文本框时弹出的列表的背景色或文本?您可以参考以下代码: 在iOS中自定义Xamarin.Forms选择器弹出列表,forms,xamarin,xamarin.forms,background,picker,Forms,Xamarin,Xamarin.forms,Background,Picker,我知道如何创建自定义渲染器来自定义Xamarin forms picker的实际文本,但如何自定义(比如)单击picker文本框时弹出的列表的背景色或文本?您可以参考以下代码: 在iOS中 使用系统; 使用Xamarin.Forms; 使用xxx; 使用xxx.iOS; 使用UIKit; 使用Xamarin.Forms.Platform.iOS; 使用基础; [程序集:ExportRenderer(typeof(MyPicker)、typeof(MyiOSPicker))] 命名空间xxx.iO
使用系统;
使用Xamarin.Forms;
使用xxx;
使用xxx.iOS;
使用UIKit;
使用Xamarin.Forms.Platform.iOS;
使用基础;
[程序集:ExportRenderer(typeof(MyPicker)、typeof(MyiOSPicker))]
命名空间xxx.iOS
{
公共类MyiOSPicker:PickerRenderer、IUIPickerViewDelegate
{
IElementController元素控制器=>元素作为IElementController;
公共MyiOSPicker()
{
}
[导出(“pickerView:viewForRow:forComponent:reusingView:”)]
公共UIView GetView(UIPickerView-pickerView、nint行、nint组件、UIView视图)
{
UILabel label=新UILabel
{
//在这里您可以设置项目的样式!!!
TextColor=UIColor.Blue,
Text=Element.Items[(int)行].ToString(),
TextAlignment=UITextAlignment.Center,
};
退货标签;
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
UIPickerView pickerView=(UIPickerView)Control.InputView;
pickerView.WeakDelegate=this;
pickerView.BackgroundColor=UIColor.Yellow;//设置pickerView的背景色
}
}
}
}
在Android中
使用系统;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
使用xxx;
使用xxx.Droid;
使用Android.Widget;
使用Android.App;
使用System.Linq;
[程序集:ExportRenderer(typeof(MyPicker)、typeof(MyAndroidPicker))]
名称空间xxx.Droid
{
公共级MyAndroidPicker:PickerRenderer
{
IElementController元素控制器=>元素作为IElementController;
公共MyAndroidPicker()
{
}
私有AlertDialog\u对话框;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.NewElement==null | | e.OldElement!=null)
返回;
Control.Click+=Control\u Click;
}
受保护的覆盖无效处置(布尔处置)
{
Control.Click-=Control\u Click;
基地。处置(处置);
}
私有无效控件\u单击(对象发送方,事件参数e)
{
选择器模型=元件;
var picker=newnumberpicker(上下文);
if(model.Items!=null&&model.Items.Any())
{
//在这里设置样式
picker.MaxValue=model.Items.Count-1;
picker.MinValue=0;
SetBackgroundColor(Android.Graphics.Color.Yellow);
picker.SetDisplayedValues(model.Items.ToArray());
picker.WrapSelectorWheel=假;
picker.Value=model.SelectedIndex;
}
变量布局=新线性布局(上下文){Orientation=Orientation.Vertical};
布局。添加视图(选择器);
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty,true);
var builder=新建AlertDialog.builder(上下文);
builder.SetView(布局);
builder.SetTitle(model.Title??);
builder.SetNegativeButton(“取消”,(s,a)=>
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty,false);
//当焦点改变时,页面内容可能会改变。
//在这种情况下,我们将失去控制。
控件?.ClearFocus();
_dialog=null;
});
builder.SetPositiveButton(“确定”,(s,a)=>
{
ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty,Picker.Value);
//可以在SelectedIndexChanged上更改页面内容。
//在这种情况下,元素和控件将不再存在。
if(元素!=null)
{
如果(model.Items.Count>0&&Element.SelectedIndex>=0)
Control.Text=model.Items[Element.SelectedIndex];
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty,false);
//当焦点改变时,页面内容也可能改变。
//在这种情况下,我们将失去控制。
控件?.ClearFocus();
}
_dialog=null;
});
_dialog=builder.Create();
_dialog.DismissEvent+=(ssender,args)=>
{
ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty,false);
};
_dialog.Show();
}
}
}
除了清除我的所有绑定值外,此操作有效。它不是在选择器中使用数字,而是使用文本标题。所以不是0,1,2,3。我有“胡萝卜、苹果等”,效果很好。忽略上面的注释。我无法在对话框中选择项目。你能帮帮我吗?
using System;
using Xamarin.Forms;
using xxx;
using xxx.iOS;
using UIKit;
using Xamarin.Forms.Platform.iOS;
using Foundation;
[assembly:ExportRenderer(typeof(MyPicker), typeof(MyiOSPicker))]
namespace xxx.iOS
{
public class MyiOSPicker:PickerRenderer,IUIPickerViewDelegate
{
IElementController ElementController => Element as IElementController;
public MyiOSPicker()
{
}
[Export("pickerView:viewForRow:forComponent:reusingView:")]
public UIView GetView(UIPickerView pickerView, nint row, nint component, UIView view)
{
UILabel label = new UILabel
{
//here you can set the style of item!!!
TextColor = UIColor.Blue,
Text = Element.Items[(int)row].ToString(),
TextAlignment = UITextAlignment.Center,
};
return label;
}
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
UIPickerView pickerView = (UIPickerView)Control.InputView;
pickerView.WeakDelegate = this;
pickerView.BackgroundColor = UIColor.Yellow; //set the background color of pickerview
}
}
}
}
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using xxx;
using xxx.Droid;
using Android.Widget;
using Android.App;
using System.Linq;
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyAndroidPicker))]
namespace xxx.Droid
{
public class MyAndroidPicker:PickerRenderer
{
IElementController ElementController => Element as IElementController;
public MyAndroidPicker()
{
}
private AlertDialog _dialog;
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (e.NewElement == null || e.OldElement != null)
return;
Control.Click += Control_Click;
}
protected override void Dispose(bool disposing)
{
Control.Click -= Control_Click;
base.Dispose(disposing);
}
private void Control_Click(object sender, EventArgs e)
{
Picker model = Element;
var picker = new NumberPicker(Context);
if (model.Items != null && model.Items.Any())
{
// set style here
picker.MaxValue = model.Items.Count - 1;
picker.MinValue = 0;
picker.SetBackgroundColor(Android.Graphics.Color.Yellow);
picker.SetDisplayedValues(model.Items.ToArray());
picker.WrapSelectorWheel = false;
picker.Value = model.SelectedIndex;
}
var layout = new LinearLayout(Context) { Orientation = Orientation.Vertical };
layout.AddView(picker);
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, true);
var builder = new AlertDialog.Builder(Context);
builder.SetView(layout);
builder.SetTitle(model.Title ?? "");
builder.SetNegativeButton("Cancel ", (s, a) =>
{
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
// It is possible for the Content of the Page to be changed when Focus is changed.
// In this case, we'll lose our Control.
Control?.ClearFocus();
_dialog = null;
});
builder.SetPositiveButton("Ok ", (s, a) =>
{
ElementController.SetValueFromRenderer(Picker.SelectedIndexProperty, picker.Value);
// It is possible for the Content of the Page to be changed on SelectedIndexChanged.
// In this case, the Element & Control will no longer exist.
if (Element != null)
{
if (model.Items.Count > 0 && Element.SelectedIndex >= 0)
Control.Text = model.Items[Element.SelectedIndex];
ElementController.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
// It is also possible for the Content of the Page to be changed when Focus is changed.
// In this case, we'll lose our Control.
Control?.ClearFocus();
}
_dialog = null;
});
_dialog = builder.Create();
_dialog.DismissEvent += (ssender, args) =>
{
ElementController?.SetValueFromRenderer(VisualElement.IsFocusedProperty, false);
};
_dialog.Show();
}
}
}