C# 对象为NULL的NSNotificationCenter.PostNotificationName()不会触发:错误还是按设计?
我使用如下代码订阅自己的通知:C# 对象为NULL的NSNotificationCenter.PostNotificationName()不会触发:错误还是按设计?,c#,xamarin.ios,nsnotificationcenter,C#,Xamarin.ios,Nsnotificationcenter,我使用如下代码订阅自己的通知: NSNotificationCenter.DefaultCenter.AddObserver("BL_UIIdleTimerFired", delegate { Console.WriteLine("BaseFolderViewController: idle timer fired"); }); 要发送通知,请执行以下操作: NSNotificationCenter.DefaultCenter.PostNotificationName("BL_UIId
NSNotificationCenter.DefaultCenter.AddObserver("BL_UIIdleTimerFired", delegate {
Console.WriteLine("BaseFolderViewController: idle timer fired");
});
要发送通知,请执行以下操作:
NSNotificationCenter.DefaultCenter.PostNotificationName("BL_UIIdleTimerFired", null);
但是,只有当PostNotificationName(string sString,object anObject)
的“anObject”参数不为空时,才能正确接收通知
这是故意的吗?我必须通过一个物体吗?还是一只虫子?
我真的不想发送对特定对象的引用。我认为这是出于设计。苹果关于另一个重载()的文档说明userInfo参数可以为null。所以我认为另外两个不能为空
“anObject”参数是发布通知的对象(发送者)以及可以从NSNotification类的对象参数中检索的对象。这是MonoTouch中的一个错误。NSNotification是这样构建的,您可以发送可选字典和可选对象(通常是发送方,但也可以是其他对象)。这两个参数都可以为null,但在MonoTouch中,传递null作为对象参数会导致null指针异常 iOS文档中关于对象参数的内容非常清楚: 与通知关联的对象。这通常是发布此通知的对象。可能是零
public void SendNotification()
{
NSNotification notification = NSNotification.FromName("AwesomeNotification",new NSObject());
NSNotificationCenter.DefaultCenter.PostNotification(notification);
}
public void StartListeningForNotification()
{
NSString name = new NSString("AwesomeNotification");
NSNotificationCenter.DefaultCenter.AddObserver(this,new Selector("AwesomeNotificationReceived:"),name,null);
}
public void StopListeningForNotification()
{
NSNotificationCenter.DefaultCenter.RemoveObserver(this,"AwesomeNotification",null);
}
[Export("AwesomeNotificationReceived:")]
public void AwesomeNotificationReceived(NSNotification n)
{
}