Question

I'm an Objective-C developer porting an application to the .Net world. In my Obj-C application, I use NSNotification objects to communicate asynchronously between a handful of objects. Is there some way to do something similar in the .Net world (more specifically, using the C# language)? The fundamental approach is that one object posts a notification that one or more objects listen for.

There's probably an obvious way of doing this, but I haven't found it yet...

Was it helpful?

Solution

Using Delegates in C# or VB.NET is the equivalent language integrated feature. You can create events by defining or using a predefined Delegate to define the "notification" that can be published and subscribed to. Then define an event on a class that you can subscribe to and raise. The MSDN documentation has a good overview once you find it here. A good example is here.

OTHER TIPS

You probably use NSNotification with NSNotificationCenter which is implementation of Event Aggregator pattern, it is implemented in Prism library

With xcode's NSNotification you can post a message which can be receive by any class as long as you have implement the NSNotification object inside the class which is to listen for the posts.... And declare the observer ;)

Here's my theory you could try out... In C# you could try creating your own notification centre by constructing a static class containing static events. Then in your other classes, you just create an instance of the events. by doing this you could recreate the same behaviour as Xcode's NSNotificationCenter.

Have you also tried this? http://www.codeproject.com/Tips/438582/Csharp-NotificationCenter?msg=4398005#xx4398005xx

sorry for any typo's! Replying by phone is awkward!

Use NotificationCenterlibrary in Nuget Package Manager. This library works like Objective-C and Swift NSNotificationCenter. A notification dispatch mechanism that enables the broadcast of information to registered observers.

PM> Install-Package NotificationCenter

Github :

https://github.com/Husseinhj/NotificationCenter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top