gpt4 book ai didi

c# - iOS Xamarin.Forms 的选择器渲染器?

转载 作者:行者123 更新时间:2023-11-28 19:28:53 25 4
gpt4 key购买 nike

enter image description here

我是 Xamarin.Forms 编程的新手。想自定义/更改 Picker 的方式

图中:-带有文本“选择”的按钮,单击时调用 picker.Focus()-选择按钮后面有黑色背景和文本颜色的选择器-空 ListView -选择器选项轮 Pane ,当选择器“聚焦”时弹出

选择器实现代码:

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);
}

代码自定义了“框”,用户单击该框可将选择器选项轮 Pane 显示在 View 中,但没有(或至少看起来没有)提供任何用于自定义选择器 Pane 的选项。

如何将选择器滚轮弹出 Pane 放置在 ListView 中?如何更改选择器轮内选项的字体大小以及更改显示选项的 Pane 的高度?

tl;dr - 如何更改选择器 Pane 高度、项目字体大小、 Pane 位置?

这就是我所得到的...

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

最佳答案

iOS 中的选择器轮称为 UIPickerView 我们可以在渲染器中获取此控件,例如:

UITextField textField = Control;
UIPickerView pickerView = textField.InputView as UIPickerView;

然后我们可以使用 Delegate 自定义它。

首先我们应该得到你的情况下的 dataSource 我们可以这样得到它:

Picker myPicker = Element;
itemList = myPicker.Items.ToList();

其次创建委托(delegate)并将列表设置为参数:

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);
}
}

关于c# - iOS Xamarin.Forms 的选择器渲染器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48433125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com