我试图捕捉和的元素时处理事件UITableViewSource
被点击。
这里是我的代码:
public class TableViewSource : UITableViewSource
{
public event EventHandler<SongSelectedEventArgs> SongSelected;
private List<Song> rows;
public TableViewSource (List<Song> list)
{
rows = list;
}
public override int RowsInSection (UITableView tableview, int section)
{
return rows.Count;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
// raise our event
var handler = SongSelected;
if (handler != null)
handler (this, new SongSelectedEventArgs (rows[indexPath.Row]));
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = new UITableViewCell(UITableViewCellStyle.Default,"mycell");
cell.TextLabel.Text = rows[indexPath.Row].ToString();
return cell;
}
public class SongSelectedEventArgs : EventArgs
{
public Song Group { get; set; }
public SongSelectedEventArgs(Song group) : base()
{ Group = group; }
}
}
我从Xamarin,这里一段示例造型:
protected class AssetGroupTableSource : UITableViewSource
{
public event EventHandler<GroupSelectedEventArgs> GroupSelected;
protected List<ALAssetsGroup> groups;
public AssetGroupTableSource(List<ALAssetsGroup> groups) { this.groups = groups; }
public override int NumberOfSections (UITableView tableView) { return 1; }
public override int RowsInSection (UITableView tableview, int section) { return groups.Count; }
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell ("AlbumCell");
if(cell == null)
cell = new UITableViewCell (UITableViewCellStyle.Default, "AlbumCell");
cell.TextLabel.Text = groups[indexPath.Row].Name;
return cell;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
// raise our event
var handler = GroupSelected;
if (handler != null)
handler (this, new GroupSelectedEventArgs (groups[indexPath.Row]));
}
public class GroupSelectedEventArgs : EventArgs
{
public ALAssetsGroup Group { get; set; }
public GroupSelectedEventArgs(ALAssetsGroup group) : base()
{ Group = group; }
}
}
当我点击一个(类元素的Song
在表格视图),它成功地转到覆盖RowSelected
。 然而, handler
始终是null
。 什么我从导致此失败的例子代码做不同?
编辑:这是应读事件行(但不触发):
TableViewSource tableData = new TableViewSource(results);
tableData.SongSelected += (object sender, TableViewSource.SongSelectedEventArgs e) => {
//AssetEnumerationScreen assetScreen = new AssetEnumerationScreen (e.Group.Name, assetGroups[e.Group]);
//NavigationController.PushViewController (assetScreen, true);
Console.WriteLine("Test");
};
编辑:这是大多数主体代码:
TableViewSource tableData = new TableViewSource(results);
tableData.SongSelected += (object sender, TableViewSource.SongSelectedEventArgs e) => {
Console.WriteLine("Test");
};
tableData.SongSelected += delegate(object sender, TableViewSource.SongSelectedEventArgs e) {
Console.WriteLine("Test");
LyricsScreen assetScreen = new LyricsScreen (e.Song);
NavigationController.PushViewController (assetScreen, true);
};
searchBool.ValueChanged += (sender, e) => {
UpdateDisplay(tableData, songList);
};
lyricSearch.TextChanged += (sender, e) => {
UpdateDisplay(tableData, songList);
};
lyricSearch.Text = String.Empty;
this.searchResults.Source = tableData;
编辑!:我发现了错误。 我有这样一行:
tableData = new TableViewSource(results);
的两倍,因此它赞同第一个事件,但后来我创建了一个新的,并设置一个源,覆盖一个与订阅。
谢谢!