C# Xamarin Forms 调用自定义ContentView事件时EventHandler总为null问题问询
问题原因
你在Reload方法的遍历逻辑中重复实例化了InstrumentCard控件:
instruments.ForEach(instr => { // 第一个实例:完成了事件绑定 InstrumentCard ic = new InstrumentCard(instr); ic.DeleteClicked += Ic_DeleteClicked; ic.EditClicked += Ic_EditClicked; // 第二个实例:未绑定任何事件,被直接添加到布局 stcInstruments.Children.Add(new InstrumentCard(instr)); });
界面上实际渲染的是未绑定事件回调的第二个实例,因此点击按钮时EventHandler始终为null。
修复方案
直接将已经完成事件绑定的ic实例添加到布局容器即可:
instruments.ForEach(instr => { InstrumentCard ic = new InstrumentCard(instr); ic.DeleteClicked += Ic_DeleteClicked; ic.EditClicked += Ic_EditClicked; stcInstruments.Children.Add(ic); });
内容的提问来源于stack exchange,提问作者Ascanio Donati
相关产品推荐
相关产品推荐

