当我在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 ());
}
}
}
答案 0 :(得分: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)