Silverlight:多次发生点击事件

时间:2014-12-29 06:25:44

标签: c# silverlight silverlight-5.0 wcf-ria-services

我在WCF ria服务(Silverlight客户端)中使用SignalR。以下是我与Hub建立连接的配置:

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 个答案:

答案 0 :(得分:1)

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

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