Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism Unsubscribe with subscription token not working

Tags:

c#

.net

prism

I am subscribing and unsubscribing to prism events using the code below in ClassA. The problem I am having is after I unsubscribe, and another completely different class, say ClassB, with a different handler registers for the same event, ClassA handler is still invoked. Why is this?

I have tried both unsubscribing using a token as well as the method delegate used when registering, both to no avail.

SubscriptionToken _subscriptionToken;

//register subscription + handler
var pevent = GetEventAggregator().GetEvent<PriceSubscriptionEvent>();
_subscriptionToken = pevent.Subscribe(r =>
{
    DataHandler(r);
    return;
}, ThreadOption.BackgroundThread, false, null);

//Unsubscribe
var pevent = GetEventAggregator().GetEvent<PriceSubscriptionEvent>();
pevent.Unsubscribe(_subscriptionToken);
like image 214
mike01010 Avatar asked Sep 06 '12 22:09

mike01010


1 Answers

I don't think the Unsubscribe can work there since you used an anonymous method on your Subscribe call. Try moving the code for subscribe in a separate method and the subscribe / unsubscribe to / from it.

like image 98
Avram Tudor Avatar answered Oct 27 '22 00:10

Avram Tudor