簡體   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