0

当我在 iPad 上运行 Xamarin monotouch 应用程序时,UIPickerView 显示,但为空。没有一个代表触发:GetRowsInComponent GetComponentCount GetTitle

我在情节提要中创建了 UIPickerView,并将其连接到文件的所有者。

这是我的 .cs 代码......

using System;
using System.Collections.Generic;
using System.Linq;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;

namespace MainApp
{
    public partial class Ultrasound_Controller : UIViewController
    {

        public Ultrasound_Controller(IntPtr handle)
            : base(handle)                   
        {
        }

        Presets_File_Picker_Model presets_file_picker_model;

        public override void ViewDidLoad()
        {
            Console.WriteLine("ViewDidLoad");
           base.ViewDidLoad();
          }



        public class Presets_File_Picker_Model : UIPickerViewModel
        {
            public event EventHandler<EventArgs> ValueChanged;

            /// <summary>
            /// The color we wish to display
            /// </summary>
            public List<string> Items
            {
                get { return items; }
                set { items = value; }
            }
            List<string> items = new List<string>();

            /// <summary>
            /// The current selected item
            /// </summary>
            public string SelectedItem
            {
                get { return items[selectedIndex]; }
            }
            protected int selectedIndex = 0;

            /// <summary>
            /// default constructor
            /// </summary>
            public Presets_File_Picker_Model()
            {
            }








            public override int GetRowsInComponent(UIPickerView picker, int component)
            {
                Console.WriteLine("GetRowsInComponent");
                return 5;
            }

            public override int GetComponentCount(UIPickerView picker)
            {
                Console.WriteLine("GetComponentCount");
                return 1;
            }

            public override string GetTitle(UIPickerView picker, int row, int component)
            {

                Console.WriteLine("GetTitle");
                return "Component " + row.ToString();
            }


            /// <summary>
            /// called when a row is selected in the spinner
            /// </summary>
            public override void Selected (UIPickerView picker, int row, int component)
            {
                Console.WriteLine("Selected");
                selectedIndex = row;
                if (this.ValueChanged != null)
                {
                    this.ValueChanged (this, new EventArgs ());
                }   
            }
        }
4

1 回答 1

0

我在任何地方都看不到您的Presets_File_Picker_Model. 在ViewDidLoad你应该有类似的东西:

this.presets_file_picker_model = new Presets_File_Picker_Model() { Items = new string[] { "one", "two", "three", "four", "five" } };
this.presets_file_picker_model.ValueChanged += HandleValueChanged;
UIPickerView pickerView = new UIPickerView();
pickerView.Model = this.presets_file_picker_model;

然后在您的代码中实现处理程序方法。

HandleValueChanged(object sender, GenericEventArgs<EventArgs> e)
于 2013-10-25T11:15:38.800 回答