Xamarin.iOS中是否提供iOS Picker?我一直在搜索,但既没有例子,也没有建立信息;但是,它可以在Xamarin.Form中找到。
答案 0 :(得分:11)
UIPickerView :( iOS SDK)
的真实快速示例将UIPickerView
添加到名为slotMachineView
的xib或Storyboard:
using System;
using UIKit;
namespace Slots
{
public partial class ViewController : UIViewController
{
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
slotMachineView.Model = new StackOverflowModel (selectedLbl);
}
public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
}
}
public class StackOverflowModel : UIPickerViewModel
{
static string[] names = new string [] {
"pscorlib.dll",
"pscorlib_aot.dll",
"Mono.PlayScript.dll",
"PlayScript.Dynamic.dll",
"PlayScript.Dynamic_aot.dll",
"PlayScript.Optimization.dll",
"playshell.exe",
"psc.exe"
};
UILabel lbl;
public StackOverflowModel (UILabel lbl)
{
this.lbl = lbl;
}
public override nint GetComponentCount (UIPickerView v)
{
return 3;
}
public override nint GetRowsInComponent (UIPickerView pickerView, nint component)
{
return names.Length;
}
public override string GetTitle (UIPickerView picker, nint row, nint component)
{
switch (component) {
case 0:
return names [row];
case 1:
return row.ToString ();
case 2:
return new string ((char)('A' + row), 1);
default:
throw new NotImplementedException ();
}
}
public override void Selected (UIPickerView picker, nint row, nint component)
{
lbl.Text = String.Format ("{0} : {1} : {2}",
names [picker.SelectedRowInComponent (0)],
picker.SelectedRowInComponent (1),
picker.SelectedRowInComponent (2));
}
public override nfloat GetComponentWidth (UIPickerView picker, nint component)
{
if (component == 0)
return 220f;
else
return 30f;
}
}
}
答案 1 :(得分:1)
是的。本机iOS控件称为UIPickerView
答案 2 :(得分:0)
public class GenderPicker : UIViewController
{
public TestVC() { }
UITextField SelectGenderTextField = new UITextField();
UIPickerView GenderPicker = new UIPickerView();
public override void ViewDidLoad()
{
base.ViewDidLoad();
AddTextField();
GenderTextField();
Constraint();
}
}
文本字段显示所选数据
private void AddTextField()
{
SelectGenderTextField.Placeholder = "Select Gender";
SelectGenderTextField.Layer.BorderWidth = 1;
SelectGenderTextField.Layer.BorderColor = UIColor.Black.CGColor;
SelectGenderTextField.Layer.MasksToBounds = true;
SelectGenderTextField.Layer.SublayerTransform = CATransform3D.MakeTranslation(5, 0, 0); //to Create a Space At The beginning of the text field
SelectGenderTextField.InputView = GenderPicker; //To Start The UIPickerView from The bottom.
}
性别挑衅 r
private void GenderTextField()
{
var genderList = new List<string> {
"Male","Female"
};
var picker = new GenderPickerModel(genderList);
GenderPicker.Model = picker;
picker.ValueChanged += (sender, e) => {
SelectGenderTextField.Text = picker.SelectedGenderByUser; //Update The Selected Value In the TextField
View.EndEditing(true);// To Dismiss the Picker View Once The User Select The Value
};
}
约束
// Used Cirrious.FluentLayouts.Touch For Constraints
private void Constraint()
{
View.AddSubviews(SelectGenderTextField);
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(
SelectGenderTextField.WithRelativeWidth(View, 0.80f),
SelectGenderTextField.WithRelativeHeight(View, 0.05f),
SelectGenderTextField.WithSameCenterX(View),
SelectGenderTextField.WithSameCenterY(View)
);
}
选择器模型类
class GenderPickerModel : UIPickerViewModel
{
public EventHandler ValueChanged;
public string SelectedGenderByUser;
private List<string> genderList;
public GenderPickerModel(List<string> genderList)
{
this.genderList = genderList;
}
public override nint GetRowsInComponent(UIPickerView pickerView, nint component)
{
return genderList.Count;
}
public override nint GetComponentCount(UIPickerView pickerView)
{
return 1;
}
public override string GetTitle(UIPickerView pickerView, nint row, nint component)
{
return genderList[(int)row];
}
public override void Selected(UIPickerView pickerView, nint row, nint component)
{
var gender = genderList[(int)row];
SelectedGenderByUser = gender;
ValueChanged(null,null);
}
}