1

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; }
        }
    }

テーブル ビューで( class のSong) 要素をクリックすると、正常に override に移動します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);

最初のものをイベントにサブスクライブしますが、新しいものを作成してソースに設定、サブスクリプションで上書きします。

ありがとう!

4

0 に答える 0