[英]C# - SignalR How can I remove my event handler
我是一个C#新手试图在我的Xamarin IOS应用程序中实现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); };
任何帮助将非常感激。
您正在使用集线器,为什么不使用内置的开/关进行方法调用?
又名:
var doSomething = feeHub.On<int>("doSomething", val => {
// Do something with int val
});
然后删除它你可以做:
doSomething.Dispose();
如果您真的想要收听流经集线器的所有数据,那么使用Received是正确的方法,@ Dracanus的答案将起作用。
我可能错了,但如果你这样做,它实际上不会取消订阅该事件。
它没有在我写的一个小测试应用程序中。
而是创建一个功能,如
void Connection_Recieved(string obj)
{
}
并做了connection.Recieved + = Connection_Recieved; 和connection.Recieved - = Connection_Recieved;
我不认为匿名事件功能是这里的方式:)
我假设,看看你可以做的代码示例,
connection.Recieved += OnReceiveData;
connection.Recieved -= OnReceiveData;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.