如何从另一个类访问列表视图

本文关键字:访问 列表 视图 另一个 | 更新日期: 2023-09-27 18:32:26

我有一个WCF服务库和客户端[Winform],它们使用netTCPbinding进行连接。
客户端调用了一个函数Search(string txtSearch,string path)服务器应该发回结果,但我不希望客户端等待结果,我需要服务器独立发送回去。因此,每当服务器发回一个项目时,客户端都应该将其添加到 listView1 中。

这是客户端应用程序的一些代码,那么如果可能的话,我怎么需要从Callback类访问 listView1!

public partial class Form1 : Form
{
    FileManagerClient client;
    public Form1()
    {
        InitializeComponent();
        InstanceContext ctx = new InstanceContext(new Callback());
        client = new FileManagerClient(ctx);
    }
}
class Callback : IFileManagerCallback
{
   public void Folder(_Folder folder)
   {
         ListViewItem item = new ListViewItem();
         item.Text = folder.Name;
         item.ToolTipText = folder.Path;
         item.Tag = item.ImageIndex;
         item.Name = item.Text;
         //Add item to listView1
   }
   public void File(_File file)
   {
         ListViewItem item = new ListViewItem();
         item.Text = file.Name;
         item.ToolTipText = file.Path;
         item.Tag = item.ImageIndex;
         item.Name = item.Text;
         //Add item to listView1
   }
}

如何从另一个类访问列表视图

实现事件

 public partial class Form1 : Form
{
    FileManagerClient client;
    public Form1()
    {
        InitializeComponent();
        Callback callback = new Callback();
        //Subscribe event for notification
        callback.OnDataReceivedEvent +=new Callback.OnDataReceived(callback_OnDataReceivedEvent);
        InstanceContext ctx = new InstanceContext(callback);
        client = new FileManagerClient(ctx);
    }
    private void callback_OnDataReceivedEvent(object sender, ListViewItem item)
    {
        //Add item here
    }
}
class Callback : IFileManagerCallback
{
    private delegate void OnDataReceived(object sender, ListViewItem item);
    private OnDataReceived _dataReceivedHandler = null;
    public event OnDataReceived OnDataReceivedEvent {
        add { _dataReceivedHandler += value; }
        remove { _dataReceivedHandler -= value; }
    }
    public void Folder(_Folder folder)
    {
        ListViewItem item = new ListViewItem();
        item.Text = folder.Name;
        item.ToolTipText = folder.Path;
        item.Tag = item.ImageIndex;
        item.Name = item.Text;
        RaiseEvents(item);
        //Add item to listView1
    }
    public void File(_File file)
    {
        ListViewItem item = new ListViewItem();
        item.Text = file.Name;
        item.ToolTipText = file.Path;
        item.Tag = item.ImageIndex;
        item.Name = item.Text;
        RaiseEvents(item);
        //Add item to listView1
    }
    private void RaiseEvents(ListViewItem item)
    {
        if (_dataReceivedHandler != null)
        {
            _dataReceivedHandler(this, item);
        }
    }
}

由于我需要从 Form1 访问许多其他内容.cs那么我认为这是最好的方法。

课程.cs:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run((MainForm = new Form1()));
    }
    public static Form1 MainForm;
}

我将 listView1 修饰符更改为公共,因此我可以从任何类访问 Form1 公共项目:

   public void Folder(_Folder folder)
    {
        ListViewItem item = new ListViewItem();
        item.Text = folder.Name;
        item.ToolTipText = folder.Path;
        item.Tag = item.ImageIndex;
        item.Name = item.Text;
        item.Group = Program.MainForm.listView1.Groups[1];
        item.ImageIndex = Program.MainForm._iconListManager.AddFolderIcon(folder.Path);
        Program.MainForm.listView1.Items.Add(item);
    }
    public void File(_File file)
    {
        ListViewItem item = new ListViewItem();
        item.Text = file.Name;
        item.ToolTipText = file.Path;
        item.Tag = item.ImageIndex;
        item.Name = item.Text;
        item.SubItems.Add(Program.MainForm.CnvrtUnit(file.Size));
        item.Group = Program.MainForm.listView1.Groups[0];
        item.ImageIndex = Program.MainForm._iconListManager.AddFileIcon(file.Path);
        Program.MainForm.listView1.Items.Add(item);
    }