NSNotification 的 C#/.Net 等效项
我是一名 Objective-C 开发人员,正在将应用程序移植到 .Net 世界。 在我的 Obj-C 应用程序中,我使用 NSNotification 对象在少数对象之间进行异步通信。 在 .Net 世界中是否有某种方法可以做类似的事情(更具体地说,使用 C# 语言)? 基本方法是一个对象发布一个通知,由一个或多个对象监听。
可能有一种明显的方法可以做到这一点,但我还没有找到......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 C# 或 VB.NET 中使用委托是等效的语言集成功能。 您可以通过定义或使用预定义的委托来定义可以发布和订阅的“通知”来创建事件。 然后在您可以订阅和引发的类上定义一个事件。 此处可以找到 MSDN 文档,其中提供了很好的概述。 此处就是一个很好的例子。
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.
您可能将 NSNotification 与 NSNotificationCenter 一起使用,它是 事件聚合器模式 的实现,它是 在 Prism 库中实现
You probably use NSNotification with NSNotificationCenter which is implementation of Event Aggregator pattern, it is implemented in Prism library
使用 xcode 的 NSNotification,您可以发布任何类都可以接收的消息,只要您在类中实现了 NSNotification 对象即可侦听帖子......并声明观察者;)
这是我的理论,您可以尝试出去...
在 C# 中,您可以尝试通过构造包含静态事件的静态类来创建自己的通知中心。 然后在其他类中,您只需创建事件的实例。 通过这样做,您可以重新创建与 Xcode 的 NSNotificationCenter 相同的行为。
你也尝试过这个吗? http://www.codeproject.com/Tips/438582/Csharp -NotificationCenter?msg=4398005#xx4398005xx
抱歉,有任何拼写错误! 打电话回复很尴尬!
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!
在 Nuget 包管理器中使用
NotificationCenter
库。 该库的工作方式类似于 Objective-C 和 Swift NSNotificationCenter。 一种通知调度机制,可以向注册观察者广播信息。Github:
https://github.com/Husseinhj/NotificationCenter
Use
NotificationCenter
library 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.Github :
https://github.com/Husseinhj/NotificationCenter