c# - Xamarin NSNotificatioCenter : How can I get the NSObject being passed?

标签 c# ipad xamarin.ios nsnotificationcenter

我正在尝试使用 NSNotificationCenter 在 View 中将通知从我的应用程序发布到另一个应用程序。所以在我的目标类中,我按如下方式创建我的观察者:

NSNotificationCenter.DefaultCenter.AddObserver ("ChangeLeftSide", delegate {ChangeLeftSide(null);});

我有我的方法:

public void ChangeLeftSide (UIViewController vc)
{
    Console.WriteLine ("Change left side is being called");
}

现在我从另一个 UIViewController 发布通知如下:

NSNotificationCenter.DefaultCenter.PostNotificationName("ChangeLeftSide", this);

如何访问目标类中的发布通知中传递的 View Controller ?在 iOS 中,它非常简单,但我似乎无法在 monotouch (Xamarin) 中找到自己的方式......

最佳答案

当你AddObserver ,您想以稍微不同的方式进行操作。尝试以下操作:

NSNotificationCenter.DefaultCenter.AddObserver ("ChangeLeftSide", ChangeLeftSide);

和你的声明 ChangeLeftSide符合 Action<NSNotification> 的方法预计 AddObserver - 给你实际的 NSNotification目的。 :

public void ChangeLeftSide(NSNotification notification)
{
    Console.WriteLine("Change left side is being called by " + notification.Object.ToString());
}

所以当你PostNotificationName ,您将 UIViewController 对象附加到通知,可以在 NSNotification 中检索该对象通过 Object属性(property)。

关于c# - Xamarin NSNotificatioCenter : How can I get the NSObject being passed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15486010/

相关文章:

android - 是否有适合 Android 开发的首选 Linux 发行版?

xamarin.ios - 是/否确认UIAlertView

c# - registry key getvalue 获取不到最新值

c# - 使用属性扩展密封类

C# 同时使用 2 个 sqldatareader?

ios - 我的游戏在各种设备上的性能测试

c# - 表单中的动态属性

iphone - 在 Storyboard 中隐藏导航栏

android - Xamarin 为原生开发者构建跨平台 sdk

ios - 企业应用程序无法在 iPad 上下载