简体   繁体   English

C# - SignalR如何删除我的事件处理程序

[英]C# - SignalR How can I remove my event handler

I am a C# newbie trying to implement SignalR in my Xamarin IOS app. 我是一个C#新手试图在我的Xamarin IOS应用程序中实现SignalR。

My code is quite simple: 我的代码很简单:

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

feedHub = _connection.CreateHubProxy ("feedHub");

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

_connection.Start ();   

my question is how can I remove my delegate? 我的问题是如何删除我的代表? Is it enough to write? 写得够吗?

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

Any help would be really appreciated. 任何帮助将非常感激。

You're using a hub, why not use the built in on/off for method invocations? 您正在使用集线器,为什么不使用内置的开/关进行方法调用?

aka: 又名:

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

Then to remove it you can do: 然后删除它你可以做:

doSomething.Dispose();

If you truly want to listen to ALL data that flows through the hub then using Received is the correct approach and @Dracanus' answer will work. 如果您真的想要收听流经集线器的所有数据,那么使用Received是正确的方法,@ Dracanus的答案将起作用。

I might be wrong, but if you do that it won't actually unsubscribe the event. 我可能错了,但如果你这样做,它实际上不会取消订阅该事件。

It didn't in a little test app I wrote anyways. 它没有在我写的一个小测试应用程序中。

Instead create a function such as 而是创建一个功能,如

void Connection_Recieved(string obj)
{
}

and do connection.Recieved += Connection_Recieved; 并做了connection.Recieved + = Connection_Recieved; and connection.Recieved -= Connection_Recieved; 和connection.Recieved - = Connection_Recieved;

I don't think anonymous event functions are the way to go here :) 我不认为匿名事件功能是这里的方式:)

I am assuming, looking at your code sample you could just do, 我假设,看看你可以做的代码示例,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM