我正在创建一个客户端/服务器WPF应用程序,如果客户端尚未连接,则服务器应用程序将新客户端信息添加到listview项目,或者如果已连接,则更新特定客户端的OnDataReceived信息。我得到'没有重载 - 匹配委托 - 错误',但我真的不明白为什么。有人能告诉我我做错了吗?
顺便说一下,我对服务器/客户端套接字通信很陌生,所以如果有人能指出我的某些资源,我会非常感激。
(用bkribbs回复更新)
// Error is here:
private void UpdateClientListControl()
{
if (Dispatcher.CheckAccess())
{
var lv = listBoxClientList;
listBoxClientList.Dispatcher.BeginInvoke(new UpdateClientListCallback(UpdateClientList), new object[] { this.listBoxClientList, false, null });
//No overload for 'UpdateClientList' matches delegate 'UpdateClientListCallback'
//I think the error is in how i added these additional parameters, but I tried using 'bool AlreadyConnected' and 'ClientInfo CurrentClient' and
//I get more errors 'Only assignment, call, incriment, ... can be used as a statement'
}
else
{
UpdateClientList(this.listBoxClientList);
}
}
和
// This worked fine until I added bool Alreadyconnected and CurrentClient
void UpdateClientList(ListView lv, bool AlreadyConnected=false, ClientInfo CurrentClient = null)
{
if (AlreadyConnected)
{
//Updates listview with CurrentClient information that has changed
}
else
{
//Updates listview with new client information
}
}
我是如何在OnDataReceived中使用它的?
public void OnDataReceived(IAsyncResult asyn)
{
//after receiving data and parsing message:
if(recieved data indicates already connected)
{
UpdateClientList(this.listBoxClientList, true, clientInfo);
}
else
{
UpdateClientList(this.listBoxClientList);
}
}
答案 0 :(得分:1)
你关闭了。有两个问题。
您现在提到的那个是因为您没有更新UpdateClientListCallback的委托声明,因为您添加了两个额外的参数。
现在看起来像:
delegate void UpdateClientListCallback(ListView lvi);
您需要将其更改为:
delegate void UpdateClientListCallback(ListView lvi, bool AlreadyConnected, ClientInfo CurrentClient);
您很快就会发现的其他问题是您的参数有点错误。您正在使用Dispatcher.BeginInvoke(Deletegate, Object[])
所以要解决问题,请更换:
listBoxClientList.Dispatcher.BeginInvoke(new UpdateClientListCallback(UpdateClientList), this.listBoxClientList, false, null);
with:
object[] parameters = new object[] { this.listBoxClientList, false, null };
listBoxClientList.Dispatcher.BeginInvoke(new UpdateClientListCallback(UpdateClientList), parameters);
或者是一个漂亮的衬垫:
listBoxClientList.Dispatcher.BeginInvoke(new UpdateClientListCallback(UpdateClientList), new object[] { this.listBoxClientList, false, null });