NSDistributedNotificationCenter.DefaultCenter.AddObserver在Xamarine.Mac

问题描述:

不工作我有2个应用程序,一个是由Xamarin构建,另一个是Xcode。NSDistributedNotificationCenter.DefaultCenter.AddObserver在Xamarine.Mac

现在在Xamarin项目中添加一个Observer。 NSDistributedNotificationCenter.DefaultCenter.AddObserver(“NotificationFromDMT”,NSKeyValueObservingOptions.OldNew,(obj)=> Console.WriteLine(“123”); });

而且我在Xcode项目发布通知

[NSDistributedNotificationCenter defaultCenter] postNotificationName:@ “NotificationFromDMT” 对象:无]。

然后我发现它不工作,我的Xamarin应用程序无法得到任何通知!

有人可以解决这个问题吗?

你没有改变任何对象,只需发送一个基于名字的(字符串)通知,以便使用NSKeyValueObservingOptions.Initial

NSDistributedNotificationCenter.DefaultCenter.AddObserver("NotificationFromDMT", NSKeyValueObservingOptions.Initial, (obj) => { 
      Console.WriteLine("123"); 
});