我是Xamarin.Forms编程的新手。想自定义/改变Picker的方式
在图片中: -Button with the text' Select',点击时调用picker.Focus() - 选择按钮后面的黑色背景和文本颜色的选择器 -Empty ListView - 当选择器被聚焦时,弹出选择器选项轮窗格
Picker实施代码:
Picker picker = new Picker
{
HorizontalOptions = LayoutOptions.Start,
VerticalOptions = LayoutOptions.Center,
WidthRequest = 73,
HeightRequest = 25,
BackgroundColor = Color.Black,
TextColor = Color.Black
};
List<string> myList = new List<string>();
myList.Add("OPTIONS 1");
myList.Add("OPTIONS 2");
myList.Add("OPTIONS 3");
myList.Add("OPTIONS 4");
myList.Add("OPTIONS 5");
foreach (string str in myList)
{
picker.Items.Add(str);
}
代码自定义“框”&#39;用户单击以将选取器选项轮窗格置于视图中,但不会(或至少看起来)提供用于自定义选取器窗格的任何选项。
如何将选取器轮弹出窗格放在ListView中? 如何更改选取器轮内选项的字体大小并更改显示选项的窗格的高度?
tl; dr - 如何更改选择窗格高度,项目字体大小,窗格位置?
就我而言......
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using AppName.iOS;
using AppName;
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace AppName.iOS
{
public class MyPickerRenderer : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// Unsubscribe from event handlers and cleanup any resources
}
if (e.NewElement != null)
{
// Configure the native control and subscribe to event handlers
}
}
}
}
答案 0 :(得分:2)
iOS中的选择器轮UIPickerView
我们可以在渲染器中获取此控件,如:
UITextField textField = Control;
UIPickerView pickerView = textField.InputView as UIPickerView;
然后我们可以根据您的需要使用Delegate
自定义此内容。
首先,我们应该在你的情况下得到dataSource
,我们可以得到它:
Picker myPicker = Element;
itemList = myPicker.Items.ToList();
其次创建委托并将列表设置为参数:
pickerView.Delegate = new MyPickerViewDelegate(itemList);
public class MyPickerViewDelegate: UIPickerViewDelegate
{
List<string> itemList;
public MyPickerViewDelegate(List<string> list)
{
itemList = list;
}
}
最后,我们可以开始使用以下事件进行自定义:
//Define the Font size or style
public override NSAttributedString GetAttributedTitle(UIPickerView pickerView, nint row, nint component)
{
var text = new NSAttributedString(
itemList[(int)row],
font: UIFont.SystemFontOfSize(24),
foregroundColor: UIColor.Red,
strokeWidth: 4
);
return text;
}
//Define the row height
public override nfloat GetRowHeight(UIPickerView pickerView, nint component)
{
return 80;
}
此外,如果您想要更灵活地定制(包括展示位置),您可以使用以下方法:
public override UIView GetView(UIPickerView pickerView, nint row, nint component, UIView view)
{
UIView contentView = new UIView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 80));
UILabel label = new UILabel();
label.Frame = contentView.Bounds;
contentView.AddSubview(label);
label.Text = itemList[(int)row];
//Change the label style
label.Font = UIFont.SystemFontOfSize(24);
label.TextColor = UIColor.Red;
return contentView;
}
以下是我的自定义渲染器:
public class MyPickerRenderer : PickerRenderer
{
List<string> itemList;
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
Picker myPicker = Element;
itemList = myPicker.Items.ToList();
UITextField textField = Control;
UIPickerView pickerView = textField.InputView as UIPickerView;
pickerView.Delegate = new MyPickerViewDelegate(itemList);
}
}
答案 1 :(得分:0)
您必须创建自己的UiPickerView
实现。这将允许您使用this guide或此设置字体大小。至于高度和位置;看起来你可以set the placement,但高度有点困难,我只能在目标C或swift中找到例子。
祝你好运!