繁体   English   中英

C#:ListView不会添加来自另一个类的项目

[英]C#: ListView does not add items that come from another Class

美好的一天,

我是整个编程领域的新手,无法找出解决方案。 我一直试图用来自另一个类的项目填充ListView。 但是,当Class2返回Array时,ListView似乎没有执行任何操作。

这是我的方法:

1)我搜索一些东西并将值发送给Class2 (Form1中的Main Class):

private void tbSearch_KeyDown(object sender, KeyEventArgs e)
    {
        Class2 cl2 = new Class2();

        if (e.KeyCode == Keys.Enter)
        {
            if (string.IsNullOrEmpty(tbSearch.Text))
            {
                MessageBox.Show("The Textbox is Empty.....");
            }
            else
            {
               listV.Items.Clear(); 
               cl2.Search(tbSearch.Text);  // Give a value to Method in Class2
            }
        }
    }

2)然后在Class2我进行了一些Internet搜索并返回带有结果的arrays

public async void Search(string search_value)
    {
            /*** Read the XML Data that i've got from Response ****/

            foreach (XmlNode node in XMLlist)
            {
                /****** Get the values that i need and write them into an array *******/

                string[] result = new string[10];

                result[0] = series_title;
                result[1] = series_type;
                result[2] = series_episodes;
                result[3] = series_score;
                result[4] = series_id;
                result[5] = series_title_english;
                result[6] = series_status;
                result[7] = series_start_date;
                result[8] = series_end_date;
                result[9] = series_image;

                Form1 fm1 = new Form1();
                fm1.Update_SearchList(result); // Return an array to Form1
            }
    }

3)最后,我尝试用返回的array (再次是Form1)填充ListView:

public void Update_SearchList(string [] array)
    {
        ListViewItem item = new ListViewItem(array);
        this.listV.BeginUpdate();                
        this.listV.Items.Add(item); // Here the Item.Add() Method doesn't add new Items to the ListView or they just aren't being shown.
        this.listV.EndUpdate();          
    }

listView.View设置为Details并且在Form.Load上生成列。

我执行listView.Items.Add();是否有问题listView.Items.Add(); 方法或还有其他问题吗?

当我尝试在一个单独的private void tbSearch_KeyDown(object sender, KeyEventArgs e)完成整个操作时private void tbSearch_KeyDown(object sender, KeyEventArgs e)一切正常。

谢谢您的时间,祝您有美好的一天!

您在Form1中创建了Form1的新实例Form1 fm1 = new Form1(); ,它不是呼叫者的实例。

我建议您从Search()返回结果,并在Form1或更好的回调函数中进行处理。

我没有尝试过,但是快速旁路解决方案可以将Form1传递为对Search()函数的引用,但是TBH对我来说似乎很讨厌。

private void tbSearch_KeyDown(object sender, KeyEventArgs e) {
    //...
           cl2.Search(tbSearch.Text, ref this); // pass the ref of current instance to cl2
        }
    }
}

public async void Search(string search_value, ref Form1 frm) {
    //...
    foreach (XmlNode node in XMLlist) {
        // ...
        frm.Update_SearchList(result); // use the frm reference instead
    }
}

public void Update_SearchList(string [] array) {
    if (InvokeRequired)
        BeginInvoke(new MethodInvoker(() => Update_SearchList(array)));
    else {
        ListViewItem item = new ListViewItem(array);
        this.listV.BeginUpdate();                
        this.listV.Items.Add(item);
        this.listV.EndUpdate();  
    }        
}

注意:代码未经测试,只是一个简单的想法!!!

另外,我不认为如果在非异步函数中调用异步函数,是否可以按预期工作。

编辑:

您也可以尝试返回结果并以主要形式进行操作。 查找草稿代码,如下所示,但它已同步并且可能会阻塞您的UI线程。 再次,代码未经测试

private void tbSearch_KeyDown(object sender, KeyEventArgs e) {
    //...
            var results = cl2.Search(tbSearch.Text);
            foreach (var item in results) Update_SearchList(item);
        }
    }
}

public List<xxx> Search(string search_value) {
    //...
    var results = new List<xxx>(); // change type to the correct type
    foreach (XmlNode node in XMLlist) {
        // ...
        results.Add(result);
    }
    return results;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM