如何正确地将项添加到ObjectListView?

时间:2015-05-22 12:40:08

标签: c# collections objectlistview

另一个问题: 我们目前使用浏览功能进入ModLoader,该功能可与常规ListView一起使用。但我们需要在SubItem上显示一个Image,因此ObjectListView非常方便。

当我启动程序时,我得到了InvalidCastException,因为我没有使用SetObjects(),因此我设置了TroveSaurusMods类型的List和一个名为相同的类。添加项目工作正常,但项目不会显示在OLV中。有人可以帮我弄这个吗?之后,请帮我看图像显示器?

以下是我添加项目的代码:

private void getMods(ref ObjectListView location)
    {
        var lstItems = new List<TroveSaurusMod>();
        logger.log("Trying to add mods from TroveSaurus to the list...", Logger.logLevel.INFO);
        try
        {
            using (var webClient = new System.Net.WebClient())
            {
                var jsonWeb = webClient.DownloadString("API LINK HERE");
                string json = js.Deserialize(new JsonTextReader(new System.IO.StringReader(jsonWeb))).ToString();
                jtr = new JsonTextReader(new System.IO.StringReader(json));
                JArray a = JArray.Parse(json);
                for (int i = 0; i < a.Count; i++)
                {
                    var element = a[i];
                    JsonTextReader arRead = new JsonTextReader(new System.IO.StringReader(element.ToString()));
                    OLVListItem item = null;
                    lvBrowse.SmallImageList = imgSmall;
                    while (arRead.Read())
                    {
                        if (arRead.Value != null)
                        {
                            if (arRead.Value.ToString() == "id")
                            {
                                item = new OLVListItem(arRead.ReadAsString());
                            }
                            if (arRead.Value.ToString() == "name")
                            {
                                item.SubItems.Add(arRead.ReadAsString());
                            }
                            if (arRead.Value.ToString() == "author")
                            {
                                item.SubItems.Add(arRead.ReadAsString());
                            }
                            if (arRead.Value.ToString() == "type")
                            {
                                item.SubItems.Add(arRead.ReadAsString());
                            }
                            if (arRead.Value.ToString() == "subtype")
                            {
                                item.SubItems.Add(arRead.ReadAsString());
                            }
                            if (arRead.Value.ToString() == "fileid")
                            {
                                item.SubItems.Add(arRead.ReadAsString());
                            }
                            if (arRead.Value.ToString() == "filedate")
                            {
                                item.SubItems.Add(UnixTimeStampToDateTime(Double.Parse(arRead.ReadAsString())).ToShortDateString());
                            }
                        }
                    }
                    //item.ImageIndex = i;

                    lstItems.Add(new TroveSaurusMod(item.Text, item.SubItems[1].Text, item.SubItems[2].Text, item.SubItems[3].Text, item.SubItems[4].Text, item.SubItems[5].Text, item.SubItems[6].Text));
                    logger.log("Successfully added " + item.Text + " to the list!", Logger.logLevel.FINE);
                }
                lvBrowse.SetObjects(lstItems.ToString());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to retrieve mod list from TroveSaurus! Please check your internet connection!", "Error parsing mods");
            logger.log(ex.StackTrace.ToString(), Logger.logLevel.ERROR);
            throw;
        }
    }

我的班级TroveSaurusMods:

public class TroveSaurusMod
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public string Type { get; set; }
    public string SubType { get; set; }
    public string FileId { get; set; }
    public string FileDate { get; set; }

    public TroveSaurusMod(string id, string name, string author, string type, string subtype, string fileId, string fileDate)
    {
        Id = id;
        Name = name;
        Author = author;
        Type = type;
        SubType = subtype;
        FileId = fileId;
        FileDate = fileDate;
    }
}

我从未使用过列表,只使用过ListView。请告诉我,如果我做错了什么,我需要改变:) 感谢

编辑:找到答案......需要设置AspectName:D

1 个答案:

答案 0 :(得分:0)

好的,我刚刚发现,我必须为每列设置AspectName ...抱歉:O