C# Mono SharedPreferences|LocalSettings 替代方案

C# Mono SharedPreferences|LocalSettings alternative

我正在 Mono 中编写一个应用程序,它是一个非 phone 应用程序,我们只是在内部使用它来针对多个平台。

我希望坚持简单的应用程序设置,并且非常喜欢 SharedPreferencesAndroidLocalSettings 中使用的方法在 Windows 平台上;有谁知道 Mono 生态系统中有类似的方法吗?

Mono 支持 System.Configuration.ApplicationSettingsBase(我的最爱)。您还可以使用 .ini 文件、注册表包装器等...

设置的子类:

class MySetting : System.Configuration.ApplicationSettingsBase
{
    [UserScopedSettingAttribute]
    [DefaultSettingValueAttribute("Overflow")]
    public String Stack
    {
        get { return (String)this["Stack"]; }
        set { this["Stack"] = value; }
    }
}

用法:

class MainClass
{
    public static void Main(string[] args)
    {
        var settings = new MySetting();
        Console.WriteLine(settings.Stack); // Default value

        settings.Stack = "Not Overflowing"; // Assign new value
        settings.Save(); // Persist the setting's changes

        var settings2 = new MySetting(); // ReLoad persisted values
        Console.WriteLine(settings2.Stack);

        var settings3 = new MySetting(); // Reset values back to their defaults
        settings3.Reset();
        Console.WriteLine(settings3.Stack);
    }
}

macOS 输出:

Whosebug
Not Overflowing
Whosebug

参考:https://github.com/mono/mono/blob/aa77a6ddccd9751a7f83fb066add7baabfb84062/mcs/class/System/System.Configuration/ApplicationSettingsBase.cs