简体   繁体   English

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

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

Good day, 美好的一天,

I'm new to whole programming thing and can't figure out / find a solution. 我是整个编程领域的新手,无法找出解决方案。 I've been trying to populate my ListView with Items that come from another Class. 我一直试图用来自另一个类的项目填充ListView。 But the ListView doesn't seem to do anything when the Class2 returns an Array . 但是,当Class2返回Array时,ListView似乎没有执行任何操作。

Here is how i make it : 这是我的方法:

1 ) I search for something and send the value to Class2 ( Main Class in Form1 ): 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 ) Then in Class2 i do some internet search and return arrays with the Results : 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) And at the end i try to populate the ListView with the returned array ( Form1 again ) : 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();          
    }

The listView.View is set to Details and the columns are being generated on Form.Load . listView.View设置为Details并且在Form.Load上生成列。

Is there a problem with how i execute the listView.Items.Add(); 我执行listView.Items.Add();是否有问题listView.Items.Add(); Method or is there another problem ? 方法或还有其他问题吗?

When i try to do this whole operation in one single private void tbSearch_KeyDown(object sender, KeyEventArgs e) everythings works. 当我尝试在一个单独的private void tbSearch_KeyDown(object sender, KeyEventArgs e)完成整个操作时private void tbSearch_KeyDown(object sender, KeyEventArgs e)一切正常。

Thank you for your time and have a good day ! 谢谢您的时间,祝您有美好的一天!

You created a new instance of Form1 in Form1 fm1 = new Form1(); 您在Form1中创建了Form1的新实例Form1 fm1 = new Form1(); , which is not the instance of your caller. ,它不是呼叫者的实例。

I would suggest you return the result from Search() and process in Form1 , or better, a callback function. 我建议您从Search()返回结果,并在Form1或更好的回调函数中进行处理。

I haven't tried but a quick bypass solution could passing Form1 as a reference to the Search() function, but TBH it looks nasty to me. 我没有尝试过,但是快速旁路解决方案可以将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();  
    }        
}

Note: code not tested, just a quick idea!!! 注意:代码未经测试,只是一个简单的想法!!!

Also, I don't think if your async will work as expected if called in a non-async function. 另外,我不认为如果在非异步函数中调用异步函数,是否可以按预期工作。

Edit: 编辑:

You can also try to return results and proceed in main form. 您也可以尝试返回结果并以主要形式进行操作。 Find the draft code as follows, but it's synced and may block your UI thread. 查找草稿代码,如下所示,但它已同步并且可能会阻塞您的UI线程。 Again, code untested ! 再次,代码未经测试

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