使用Monotouch处理iOS的UITableViewSource事件

我正在尝试捕获并处理一个事件,当单击UITableViewSource的元素时。
这是我的代码:

  public class TableViewSource : UITableViewSource { public event EventHandler SongSelected; private List rows; public TableViewSource (List 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 GroupSelected; protected List groups; public AssetGroupTableSource(List 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); 

两次,所以它订阅第一个到事件,但后来我创建了一个新的,并将设置为源,覆盖订阅的一个。

谢谢!