客户端服务器套接字应用程序上的方法错误没有重载
本文关键字:错误 方法 重载 程序上 服务器 套接字 应用 应用程序 客户端 | 更新日期: 2023-09-27 18:20:29
我正在创建一个客户端/服务器WPF应用程序,如果客户端尚未连接,服务器应用程序会将新的客户端信息添加到列表视图项中,如果客户端已经连接,则会更新特定客户端的信息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);
}
}
你很接近。有两个问题。
您现在提到的问题是,自从添加了两个额外的参数后,您还没有更新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);
带有:
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 });