C# 无法强制转换图形项类型的对象
如何解决问题,请遵循以下步骤: 当我提取项目时,则获取消息错误: “无法将'System.Collections.Generic.List`1[mypro.InfoDialog+Mycontact]'类型的对象强制转换为'Mycontact' 行号处的C代码:C# 无法强制转换图形项类型的对象,c#,winforms,C#,Winforms,如何解决问题,请遵循以下步骤: 当我提取项目时,则获取消息错误: “无法将'System.Collections.Generic.List`1[mypro.InfoDialog+Mycontact]'类型的对象强制转换为'Mycontact' 行号处的C代码: public class Mycontact { public string P_DISPLAY_NAME { get; set; } public string P_AVAILABILITY
public class Mycontact
{
public string P_DISPLAY_NAME { get; set; }
public string P_AVAILABILITY { get; set; }
public string P_AVATAR_IMAGE { get; set; }
}
Mycontact fbContact;
private void AddDataToList()
{
var fbList = new List<Mycontact>();
foreach (dynamic item in result.data)
{
fbContact = new Mycontact() { P_DISPLAY_NAME = (string)item["name"], P_AVAILABILITY = (string)item["online_presence"]};
fbList.Add(fbContact);
listBox1.Items.Add(fbList);
}
}
private int mouseIndex = -1;
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1) return;
line number:
Mycontact contact = (Mycontact)listBox1.Items[e.Index];
Brush textBrush = SystemBrushes.WindowText;
if (e.Index > -1)
{
// Drawing the frame
if (e.Index == mouseIndex)
{
e.Graphics.FillRectangle(SystemBrushes.HotTrack, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}
else
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}else{
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
}
// Drawing the text
e.Graphics.DrawString(contact.P_DISPLAY_NAME, e.Font, textBrush, e.Bounds.Left + 20, e.Bounds.Top);
}
}
}
公共类Mycontact
{
公共字符串P_DISPLAY_NAME{get;set;}
公共字符串P_可用性{get;set;}
公共字符串P_AVATAR_IMAGE{get;set;}
}
接触;
私有void AddDataToList()
{
var fbList=新列表();
foreach(result.data中的动态项)
{
fbContact=new Mycontact(){P_DISPLAY_NAME=(string)项[“NAME”],P_AVAILABILITY=(string)项[“online_presence”];
fbList.Add(fbContact);
listBox1.Items.Add(fbList);
}
}
私有int mouseIndex=-1;
私有void listBox1_DrawItem(对象发送方,DrawItemEventArgs e)
{
如果(e.Index==-1)返回;
行号:
Mycontact contact=(Mycontact)列表框1.项目[e.索引];
画笔文本画笔=系统画笔.WindowText;
如果(如索引>-1)
{
//画框
如果(e.Index==鼠标索引)
{
e、 Graphics.FillRectangle(SystemBrush.HotTrack,e.Bounds);
textBrush=systembrush.HighlightText;
}
其他的
{
if((e.State&DrawItemState.Selected)=DrawItemState.Selected)
{
e、 Graphics.FillRectangle(SystemBrush.Highlight,e.Bounds);
textBrush=systembrush.HighlightText;
}否则{
e、 Graphics.FillRectangle(SystemBrush.Window,e.Bounds);
}
//绘制文本
e、 Graphics.DrawString(contact.P_DISPLAY_NAME,e.Font,textBrush,e.Bounds.Left+20,e.Bounds.Top);
}
}
}
似乎列表中的全部内容都添加到了listbox1,而不是单个项目(listbox1.Items.Add(fbList))
难道不是:
listBox1.Items.Add(fbContact);
或者,您可以在循环后设置listBox1.DataSource=fbList。似乎整个列表都添加到listBox1,而不是单个项目(listBox1.Items.Add(fbList)) 难道不是:
listBox1.Items.Add(fbContact);
或者,您可以在循环后设置listBox1.DataSource=fbList,将完整列表作为单个项目添加到listbox:
listBox1.Items.Add(fbList)代码>
那么这条线呢
(Mycontact)列表框1.项目[e.索引]代码>
返回MyContact对象的列表,而不是单个MyContact对象
所以要解决这个问题,你可以像这样在列表中添加每个联系人的联系人
listBox1.Items.Add(fbContact)代码>您正在将完整列表作为单个项目添加到列表框中:
listBox1.Items.Add(fbList)代码>
那么这条线呢
(Mycontact)列表框1.项目[e.索引]代码>
返回MyContact对象的列表,而不是单个MyContact对象
所以要解决这个问题,你可以像这样在列表中添加每个联系人的联系人
listBox1.Items.Add(fbContact)代码>在循环的每次迭代中,您都要将整个fbList添加到控件中,我觉得这不太正确。为什么不直接将MyContact实例添加到listbox中呢?在循环的每次迭代中,您都将整个fbList添加到控件中,这在我看来不太正确。为什么不直接将MyContact实例添加到listbox?是的,我可以强制转换,没有更多错误ListBox1.Items.add(value)和ListBox1.DataSource=value之间的区别?使用ListBox1.Items.add(value)可以将值添加到listbox的内部项集合中。可能是已存在的项目。通过使用datasource,可以将列表框绑定到已创建的列表。同样在这种情况下,由于listbox没有使用begin/endupdate,因此可能会提高性能,因为ui不会尝试对每个项进行自我更新。因此,当我使用DataSource时,在绘制时是否有事件更新listbox中的数据?我的意思是,例如,当他们在线或离线更改其状态时,他们的事件是否已更改或修复,而不是针对普通列表(如用于fblist),因为列表没有实现listchanged事件接口,但如果您将其改为BindingList并将其分配为数据源,则listbox将侦听更改。但是,开箱即用仅适用于添加或删除的项目。对于itemchanged事件,您应该让MyContact implement INotifyPropertyChanged并在进行更改时引发一个已更改的事件,该更改应该会更新您的UI(或任何其他侦听器)是的,我可以强制转换,无错误更多ListBox1.Items.Add(value)和ListBox1.DataSource=value?与ListBox1.Items.Add(value)之间的区别将该值添加到列表框的内部项集合中。可能是已存在的项目。通过使用datasource,可以将列表框绑定到已创建的列表。同样在这种情况下,由于listbox没有使用begin/endupdate,因此可能会提高性能,因为ui不会尝试对每个项进行自我更新。因此,当我使用DataSource时,在绘制时是否有事件更新listbox中的数据?我的意思是,例如,当他们在线或离线更改其状态时,他们的事件是否已更改或修复,而不是针对普通列表(如用于fblist),因为列表没有实现listchanged事件接口,但如果您将其改为BindingList并将其分配为数据源,则listbox将侦听更改。但是,开箱即用仅适用于添加或删除的项目。对于itemchanged事件,您应该让MyContact implement INotifyPropertyChanged并在进行更改时引发更改的事件,该更改应该会更新您的UI(或任何其他侦听器)。我想问您一个问题,我想从url绘制图像,如:“,我曾经用字节和C代码绘制图像,图像的虚拟联系人;MemoryStream流=新的MemoryStream(c