我有一個源碼數據庫表「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是空白的。我錯過了什麼嗎?我的邏輯錯誤在哪裏?
謝謝。如何使用機械臂