C# MonoTouch中的简单UIPickerView(Xamarin)?

C# MonoTouch中的简单UIPickerView(Xamarin)?,c#,ios,objective-c,xamarin.ios,uipickerview,C#,Ios,Objective C,Xamarin.ios,Uipickerview,有人能描述一下我如何使用XCode在monotouch中创建UIPickerView并用示例数据填充它吗 我确实看了这里的示例:但这并没有太大帮助,因为我正在XCode中创建UIPickerView。以下是我目前掌握的情况: public partial class StatusPickerPopoverView : UIViewController { public StatusPickerPopoverView (IntPtr handle) : base (handle)

有人能描述一下我如何使用XCode在monotouch中创建UIPickerView并用示例数据填充它吗

我确实看了这里的示例:但这并没有太大帮助,因为我正在XCode中创建UIPickerView。以下是我目前掌握的情况:

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

我相信您希望使用UIPickerViewModel实例设置UIPickerView的Model属性

pickerStatus.Model = new StatusPickerViewModel();

以下是为IList提供UIPickerViewModel的要点:

因此我基本上找到了答案,而且比你想象的要简单

必须在ViewDidLoad中设置模型,否则将崩溃。。。这就是它没有被正确填充的原因。记住:在“ViewDidLoad”中设置它


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);
        }    
        }
我创建了一个比当前答案更详细的解决方案

首先,将UIPickerView添加到情节提要中。将名称添加到UIPicker

接下来,需要一个实现UIPickerViewModel的类,该类设置填充它的选择器模型。如果您更熟悉Android开发,那么为Xamarin iOS设置选择器可能会显得很奇怪,您不能只使用列表填充选择器


最后,将UIPickerView的模型设置为创建的类

有没有人知道如何检测用户是否点击了拾取器的外部?@nhenrique也许你可以在下面的视图中添加一个手势识别器并在那里处理它,或者你可以使用一些开源库来处理它。如果您使用的是xamarin,那么该库可能不会有多大帮助。很遗憾:-在getTitle部分中,您会得到一个自定义列表吗?因为现在我得到了5次“头衔”。
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);
        }    
        }