我有一个使用netTCPbinding连接的WCF服务lib和客户端[Winform]
客户端调用函数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
}
}
答案 0 :(得分:1)
实施活动
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);
}
}
}
答案 1 :(得分:0)
因为我需要从Form1.cs访问许多其他东西,所以我认为这是最好的方法。
<强>的Program.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修饰符更改为Public,因此我可以从任何类访问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);
}