C# 是否仅向特定的动态对象触发事件?
这是到目前为止我的代码。我有在客户端连接时动态创建的主窗体和wcf对象。现在,所有wcf对象都订阅从主窗体激发的事件 然而,我希望用户能够从主窗体的组合框中选择一个名称,并仅向提交此名称的wcf对象触发一个事件 你认为做这件事最好的方法是什么 谢谢C# 是否仅向特定的动态对象触发事件?,c#,events,C#,Events,这是到目前为止我的代码。我有在客户端连接时动态创建的主窗体和wcf对象。现在,所有wcf对象都订阅从主窗体激发的事件 然而,我希望用户能够从主窗体的组合框中选择一个名称,并仅向提交此名称的wcf对象触发一个事件 你认为做这件事最好的方法是什么 谢谢 namespace server2 { public partial class Form2 : Form { public Form2() { InitializeCompon
namespace server2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public event EventHandler sendEvnt;
private ServiceHost duplex;
private void Form2_Load(object sender, EventArgs e) /// once the form loads, create and open a new ServiceEndpoint.
{
duplex = new ServiceHost(typeof(ServerClass));
duplex.AddServiceEndpoint(typeof(IfaceClient2Server), new NetTcpBinding(), "net.tcp://localhost:9080/service");
duplex.Open();
this.Text = "SERVER *on-line*";
}
private void button1_Click(object sender, EventArgs e)
{
sendEvnt(this, new EventArgs());
// this send an event to all WCF objects
// what should I do for it to send an event ONLY to the wcf object, that's name is selected from the comboBox ?
}
}
class ServerClass : IfaceClient2Server
{
IfaceServer2Client callback;
public ServerClass()
{
callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();
}
public void StartConnection(string name)
{
var myForm = Application.OpenForms["Form2"] as Form2;
myForm.comboBox1.Items.Add(name);
myForm.comboBox1.SelectedItem = name; // adds a name to form's comboBox.
myForm.sendEvnt += new EventHandler(eventFromServer); // somehow need to incorporate the 'name' here.
callback.Message_Server2Client("Welcome, " + name );
}
void eventFromServer(object sender, EventArgs e)
{
var myForm = Application.OpenForms["Form2"] as Form2;
string msg = myForm.tb_send.Text;
if (msg == "") { msg = "empty message"; }
callback.Message_Server2Client(msg);
}
public void Message_Cleint2Server(string msg)
{
}
public void Message2Client(string msg)
{
}
}
[ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)]
public interface IfaceClient2Server ///// what comes from the client to the server.
{
[OperationContract(IsOneWay = true)]
void StartConnection(string clientName);
[OperationContract(IsOneWay = true)]
void Message_Cleint2Server(string msg);
}
public interface IfaceServer2Client ///// what goes from the sertver, to the client.
{
[OperationContract(IsOneWay = true)]
void AcceptConnection();
[OperationContract(IsOneWay = true)]
void RejectConnection();
[OperationContract(IsOneWay = true)]
void Message_Server2Client(string msg);
}
}
命名空间服务器2
{
公共部分类表单2:表单
{
公共表格2()
{
初始化组件();
}
公共事件处理程序sendEvnt;
专用服务主机双工;
private void Form2_Load(object sender,EventArgs e)///表单加载后,创建并打开新的ServiceEndpoint。
{
duplex=新的ServiceHost(typeof(ServerClass));
duplex.AddServiceEndpoint(typeof(IfaceClient2Server)),新的NetTcpBinding(),“net。tcp://localhost:9080/service");
duplex.Open();
this.Text=“服务器*在线*”;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
sendEvnt(这个,新的EventArgs());
//这将向所有WCF对象发送事件
//我应该怎么做才能将事件只发送到wcf对象,即从组合框中选择名称?
}
}
类服务器类:IfaceClient2Server
{
IfaceServer2Client回调;
公共服务器类()
{
callback=OperationContext.Current.GetCallbackChannel();
}
public void StartConnection(字符串名称)
{
var myForm=Application.OpenForms[“Form2”]作为Form2;
myForm.comboBox1.Items.Add(name);
myForm.comboBox1.SelectedItem=name;//将名称添加到表单的组合框中。
myForm.sendEvnt+=neweventhandler(eventFromServer);//需要在此处合并“name”。
callback.Message_server2客户端(“欢迎,”+name);
}
void eventFromServer(对象发送方,EventArgs e)
{
var myForm=Application.OpenForms[“Form2”]作为Form2;
字符串msg=myForm.tb_send.Text;
如果(msg==“”){msg=“空消息”;}
callback.Message_server2客户端(msg);
}
公共无效消息\u Cleint2Server(字符串msg)
{
}
公共无效消息2客户端(字符串消息)
{
}
}
[ServiceContract(Namespace=“server”,CallbackContract=typeof(IfaceServer2Client),SessionMode=SessionMode.Required]
公共接口IfaceClient2Server///从客户端到服务器的内容。
{
[运营合同(IsOneWay=true)]
void StartConnection(字符串clientName);
[运营合同(IsOneWay=true)]
无效消息\u Cleint2Server(字符串msg);
}
公共接口IfaceServer2Client///从服务器到客户端的内容。
{
[运营合同(IsOneWay=true)]
void AcceptConnection();
[运营合同(IsOneWay=true)]
无效连接();
[运营合同(IsOneWay=true)]
无效消息\服务器2客户端(字符串消息);
}
}
WCF对象是如何存储的?我想你是在收集它们。如果是这种情况,请尝试将您的收藏更改为词典
。从那里,您可以根据用户输入的字符串在字典中查找对象