客户端服务器套接字应用程序上的方法错误没有重载
No overload for method error on client server sockets application
我正在创建一个 client/server WPF 应用程序,如果客户端尚未连接,服务器应用程序将新的客户端信息添加到列表视图项,或者如果他们已经连接,则更新该特定客户端的信息 OnDataReceived。我得到 'No overload for -- matches delegate -- error',但我真的不明白为什么。有人可以告诉我我做错了什么吗?
顺便说一下,我对 server/client 套接字通信还很陌生,所以如果有人能给我指出一些资源,我将不胜感激。
(更新了 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 });
我正在创建一个 client/server WPF 应用程序,如果客户端尚未连接,服务器应用程序将新的客户端信息添加到列表视图项,或者如果他们已经连接,则更新该特定客户端的信息 OnDataReceived。我得到 'No overload for -- matches delegate -- error',但我真的不明白为什么。有人可以告诉我我做错了什么吗?
顺便说一下,我对 server/client 套接字通信还很陌生,所以如果有人能给我指出一些资源,我将不胜感激。
(更新了 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 });