iOS Xamarin.Forms 的选择器渲染器?
Picker renderer for iOS Xamarin.Forms?
我是 Xamarin.Forms 编程新手。想customize/change Picker的方式
图中:
-带有文本 'Select' 的按钮,单击时调用 picker.Focus()
- select 按钮后面有黑色背景和文本颜色的选择器
-空列表视图
-The picker options wheel pane, pops up when a picker is 'Focused'
选择器实现代码:
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);
}
代码自定义 'box' 用户单击以将选择器选项轮窗格带入视图,但没有(或至少看起来)提供任何用于自定义选择器窗格的选项。
如何将选择器滚轮弹出窗格放置在 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
}
}
}
}
您将必须创建自己的 a UiPickerView
实现。这将允许您使用 this guide or this one. As for the height and placement; it appears that you can 设置字体大小,但高度有点困难,我只能在 objective C 或 swift.
中找到示例
祝你好运!
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);
}
}
我是 Xamarin.Forms 编程新手。想customize/change Picker的方式
图中: -带有文本 'Select' 的按钮,单击时调用 picker.Focus() - select 按钮后面有黑色背景和文本颜色的选择器 -空列表视图 -The picker options wheel pane, pops up when a picker is 'Focused'
选择器实现代码:
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);
}
代码自定义 'box' 用户单击以将选择器选项轮窗格带入视图,但没有(或至少看起来)提供任何用于自定义选择器窗格的选项。
如何将选择器滚轮弹出窗格放置在 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
}
}
}
}
您将必须创建自己的 a UiPickerView
实现。这将允许您使用 this guide or this one. As for the height and placement; it appears that you can
祝你好运!
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);
}
}