我有一个源码数据库表“BikeInfo”填充在IPhone应用程序从SQLite数据库的数据一个UIPickerView -
[BikeID, BikeName]
现在我想我pickerview与来自该自行车的名称来填充数据库。在.Net中非常简单。但作为MonoTouch的完全初学者,我不知道如何解决这个问题。我做了一些Google搜索,发现我必须创建一些从UIPickerViewModel
类延伸的自定义类,并覆盖它的各种方法。但就是这样。我找不到任何示例代码,教程或任何内容。我一直在为此奋战三天。我完全困惑。到目前为止,我已经编写了一个函数,用于从表中提取数据并返回一个类对象列表[我已经编写了包含属性BikeName和BikeId]。也许我正在寻找所有错误的地方。但我需要以任何方式完成这件事。
任何类型的教程或C#中处理此特定目标场景的示例代码都会对我有所帮助。
下一个目标是显示一条警告消息,显示用户触摸某个特定物品时相应的自行车ID。我认为我们必须在该自定义类中定义选定的事件。我从互联网上选择了这么多。
谢谢。
这就是我迄今为止所做的。我创建了一个包含属性BikeID
和BikeName
的类。
namespace ASTONAPP
{
public class PickerFacilityTemplate
{
public PickerFacilityTemplate()
{
}
public int BikeID{get; set;}
public string BikeName{get; set;}
}
}
之后,我在我的数据库处理程序类中写了函数,它返回上述类类的类对象列表。
public List<PickerFacilityTemplate> FetchFacility()
{
DataSet ds = new DataSet();
string sql = "select * from BikeInfo order by BikeName";
this.CreateDBConnection();
SqliteDataAdapter sda = new SqliteDataAdapter (sql, sconn);
sda.Fill (ds);
List<PickerFacilityTemplate> objfcl=new List<PickerFacilityTemplate>();
for(int i=0; i<ds.Tables[0].Rows.Count; i++)
{
PickerFacilityTemplate pft=new PickerFacilityTemplate();
pft.BikeID=Convert.ToInt32(ds.Tables[0].Rows[i]["BikeID"].ToString());
pft.BikeName=ds.Tables[0].Rows[i]["BikeName"].ToString();
objfcl.Add (pft);
}
this.CloseDBConnection();
return objfcl.ToList();
}
public class PickerDataModelSource: UIPickerViewModel
{
public PickerDataModelSource()
{
}
public PickerDataModelSource (List<PickerFacilityTemplate> lst)
{
this.Items=lst;
}
public event EventHandler<EventArgs> ValueChanged;
public List<PickerFacilityTemplate> Items;
List<PickerFacilityTemplate> _items = new List<PickerFacilityTemplate>();
public PickerFacilityTemplate SelectedItem
{
get
{
return this._items[this._selectedIndex];
}
}
public int _selectedIndex = 0;
public override int GetRowsInComponent (UIPickerView picker, int component)
{
return this._items.Count;
}
public override string GetTitle (UIPickerView picker, int row, int component)
{
return items[row].BikeName.ToString();
}
public override int GetComponentCount (UIPickerView picker)
{
return 1;
}
public override void Selected(UIPickerView picker, int row, int component)
{
this._selectedIndex = row;
if (this.ValueChanged != null)
{
this.ValueChanged (this, new EventArgs());
}
}
}
在我有pickerview,我已经写在屏幕的类文件
现在,
PickerDataModelSource _pickerSource;
,并在ViewDidLoad
方法:
this.Picker.Source=this._pickerSource;
this.Picker.Model=this._pickerSource;
但是当我运行应用程序,我的pickerview是空白的。我错过了什么吗?我的逻辑错误在哪里?
谢谢。如何使用机械臂