首页 > 解决方案 > 在设置包中的设置更改时收到通知(在 Xamarin iOS 中)

问题描述

我希望我的应用程序在设置包中的一项设置发生更改时收到通知。或者如果这不可能,任何改变(然后我会检查它是否是那个特定的设置)。

我将如何在 Xamarin iOS 中实现这一点?

似乎这些选项之一在这些答案中得到了解决。但我不知道如何在 Xamarin/C# 中做到这一点。

标签: iosxamarinxamarin.ios

解决方案


只需将 Objective-C 代码翻译成 C#,如果您阅读此处的文档,您会发现有一些示例:

// 拉姆达风格

NSNotificationCenter.DefaultCenter.AddObserver(

    NSValueTransformer.UserDefaultsDidChangeNotification, (notification) => { Console.WriteLine("Received the notification NSValueTransformer", notification); }

);

//方法风格

void Callback(NSNotification notification)
{
    Console.WriteLine("Received a notification NSValueTransformer", notification);
}

void Setup()
{
    NSNotificationCenter.DefaultCenter.AddObserver(NSValueTransformer.UserDefaultsDidChangeNotification, Callback);
}

参考:userdefaultsdidchangenotification


推荐阅读