C# MonoTouch (Xamarin) 中的简单 UIPickerView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15577389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A simple UIPickerView in MonoTouch (Xamarin)?
提问by P. Sami
Can anybody describe how I can create a UIPickerView in monotouch using XCode and populate it with sample data?
任何人都可以描述我如何使用 XCode 在单点触控中创建 UIPickerView 并用示例数据填充它?
I did look at the example here: https://github.com/xamarin/monotouch-samples/blob/master/MonoCatalog-MonoDevelop/PickerViewController.xib.csbut this hasn't been very helpful since I am creating my UIPickerView in the XCode. Here's what I have so far:
我确实在这里查看了示例:https: //github.com/xamarin/monotouch-samples/blob/master/MonoCatalog-MonoDevelop/PickerViewController.xib.cs但这并不是很有帮助,因为我在其中创建了 UIPickerView XCode。这是我到目前为止所拥有的:
public partial class StatusPickerPopoverView : UIViewController
{
public StatusPickerPopoverView (IntPtr handle) : base (handle)
{
pickerStatus = new UIPickerView();
pickerStatus.Model = new StatusPickerViewModel();
}
public StatusPickerPopoverView (): base ()
{
}
public class StatusPickerViewModel : UIPickerViewModel
{
public override int GetComponentCount (UIPickerView picker)
{
return 1;
}
public override int GetRowsInComponent (UIPickerView picker, int component)
{
return 5;
}
public override string GetTitle (UIPickerView picker, int row, int component)
{
return "Component " + row.ToString();
}
}
}
采纳答案by P. Sami
So I basically found the answer and it was waaaaaaaaaaaaaaay easier than you can think!
所以我基本上找到了答案,它比你想象的要容易!
The model has to be set in ViewDidLoad otherwise will crash... That's why it was not being populated correctly. Remember: set it up in "ViewDidLoad".
模型必须在 ViewDidLoad 中设置,否则会崩溃......这就是它没有被正确填充的原因。请记住:在“ViewDidLoad”中设置它。
public partial class StatusPickerPopoverView : UIViewController
{
public StatusPickerPopoverView (IntPtr handle) : base (handle)
{
}
public override ViewDidLoad()
{
base.ViewDidLoad();
pickerStatus = new UIPickerView();
pickerStatus.Model = new StatusPickerViewModel();
}
public class StatusPickerViewModel : UIPickerViewModel
{
public override int GetComponentCount (UIPickerView picker)
{
return 1;
}
public override int GetRowsInComponent (UIPickerView picker, int component)
{
return 5;
}
public override string GetTitle (UIPickerView picker, int row, int component)
{
return "Component " + row.ToString();
}
}
}
回答by Jacob Foshee
I believe you want to set the UIPickerView's Model property with your UIPickerViewModel instance.
我相信你想用你的 UIPickerViewModel 实例设置 UIPickerView 的模型属性。
pickerStatus.Model = new StatusPickerViewModel();
Here is a Gist that provides a UIPickerViewModel for an IList: https://gist.github.com/jfoshee/5223962
这是一个为 IList 提供 UIPickerViewModel 的 Gist:https: //gist.github.com/jfoshee/5223962
回答by A.G
Xamarin Studio 5.10:
Xamarin Studio 5.10:
public partial class ViewController : UIViewController
{
UIPickerView samplePicker;
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
samplePicker = new UIPickerView (new CoreGraphics.CGRect (10f, 244f, this.View.Frame.Width - 20, 250f));
samplePicker.BackgroundColor = UIColor.LightGray;
samplePicker.Model = new StatusPickerViewModel ();
}
public class StatusPickerViewModel : UIPickerViewModel
{
public override nint GetRowsInComponent (UIPickerView pickerView, nint component)
{
return 5;
}
public override string GetTitle (UIPickerView pickerView, nint row, nint component)
{
return "Title";
}
public override nint GetComponentCount (UIPickerView pickerView)
{
return 1;
}
}
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
this.View.AddSubview (samplePicker);
}
}
回答by cfl
I created a blog post, covering it in more detail than the current answers.
我创建了一篇博客文章,比当前的答案更详细地介绍了它。
Firstly, add a UIPickerView to your Storyboard. Add a name to the UIPicker.
首先,将 UIPickerView 添加到您的故事板。向 UIPicker 添加名称。
Next, A class implementing UIPickerViewModel is required, which sets the Pickers model which populates it. If you are more familiar with Android development, setting up a Picker for Xamarin iOS can seem weird, you cannot just populate the picker with a list.
接下来,需要一个实现 UIPickerViewModel 的类,它设置填充它的 Pickers 模型。如果您更熟悉 Android 开发,那么为 Xamarin iOS 设置选择器可能看起来很奇怪,您不能只用列表填充选择器。
Lastly, set the model of the UIPickerView to the class created.
最后,将 UIPickerView 的模型设置为创建的类。