2013-04-23 214 views
1

我是一个C#新手试图在我的Xamarin IOS应用程序中实现SignalR。C# - SignalR我如何删除我的事件处理程序

我的代码非常简单:

_connection = new Microsoft.AspNet.SignalR.Client.Hubs.HubConnection (Common.signalRAddress); 

feedHub = _connection.CreateHubProxy ("feedHub"); 

_connection.Received += data => { OnReceiveData (data); }; 

_connection.Start(); 

我的问题是我如何删除我的委托? 写足够了吗?

_connection.Received -= data => { OnReceiveData (data); }; 

任何帮助将非常感激。

+0

是的。这就是你如何删除委托人! – Dave 2013-04-23 14:54:49

+1

[在C#中取消订阅匿名方法]的可能重复(http://stackoverflow.com/questions/183367/unsubscribe-anonymous-method-in-c-sharp) – poupou 2013-04-23 15:06:25

回答

3

我可能是错的,但如果你这样做,它不会实际取消订阅该事件。

它没有在我写的一个小测试应用程序。

相反创建函数如

void Connection_Recieved(string obj) 
{ 
} 

和做connection.Recieved + = Connection_Recieved; 和connection.Recieved - = Connection_Recieved;

我不认为匿名事件功能是去这里:)

我假设的方式,看着你的代码示例,你可以只是做,

connection.Recieved += OnReceiveData; 
    connection.Recieved -= OnReceiveData; 
7

您使用的是集线器,为什么不使用内置的on/off方法调用?

又名:

var doSomething = feeHub.On<int>("doSomething", val => { 
    // Do something with int val 
}); 

然后将其删除,你可以这样做:

doSomething.Dispose(); 

如果你真的要听流经枢纽然后使用接收的所有数据是正确的做法,并@Dracanus的答案将起作用。

相关问题