2014-12-29 72 views
0

我在WCF ria服务(Silverlight客户端)中使用SignalR。下面是我的配置为建立连接到我的枢纽:Silverlight:发生多次点击事件

private void btn_click(object sender, RoutedEventArgs e) 
{ 
      var hubConnection = new HubConnection(url: "http://10.1.0.5:2096/signalr/"); 
      var chat = hubConnection.CreateHubProxy(hubName: "chat"); 
      chat.On<string>("hello", msg => System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(msg))); 
      hubConnection.Start().Wait(); 
      chat.Invoke<string>("sendMessage", "Hello!"); 
} 

枢纽:

[HubName("chat")] 
public class ChatHub : Hub 
{ 
     public void SendMessage(string message) 
     { 
      Clients.All.hello(message); 
     } 
} 

连接成功启动,但每次我按一下按钮,它激发了好几次。例如第一次启动一次,第二次启动两次,然后......
任何想法?

回答

1

您的邮件只发送一次,但是您在每次点击时注册事件处理程序。将其移出btn click事件。

var chat = hubConnection.CreateHubProxy(hubName: "chat"); 
chat.On<string>("hello", msg => System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(msg)));