方法名是预期的,而不是null, c#错误

  • 本文关键字:null 错误 方法 c# winforms
  • 更新时间 :
  • 英文 :


我有这个代码片段,在列表视图控件中设置图像。

delegate void SetImageListDataCallback(string imgName, int i);
void SetImageListData(string imgName, int i)
{
if (lvFoundModelImages.InvokeRequired)
{
SetImageListDataCallback c = new SetImageListDataCallback(imgName, i);
this.Invoke(c, new object[]
{
imgName,i
});
}
else
{
lvFoundModelImages.LargeImageList = imageList1;
lvFoundModelImages.Items.Add(new ListViewItem(imgName, i));
}
}

但问题是我在这一行得到错误:

SetImageListDataCallback c = new SetImageListDataCallback(imgName, i);

第一个参数

ImgName不为空

。而第二个参数"i"显示

方法名称

您正在使用错误的语法来创建SetImageListDataCallback的新实例。您应该传递具有匹配签名的方法作为委托构造函数的第一个参数:

SetImageListDataCallback c = new SetImageListDataCallback(SetImageListData);

然后,您可以在调用时将imgName和i参数作为参数传递:

this.Invoke(c, new object[] { imgName, i });

最新更新