1

我想在Template10应用程序中创建自定义设置。设置服务没有很好的记录,所以我想问一下创建自定义设置的最佳/推荐方式是什么,例如我想添加设置,您可以在其中打开或关闭搜索历史记录,它只是布尔值。在我使用它在应用程序中设置设置之前:ApplicationData.Current.LocalSettings.Values["SettingName"] = true;

要获得设置值,我只需使用:

(bool)ApplicationData.Current.LocalSettings.Value["SettingName"]; 
4

1 回答 1

2

看看SettingsService最小或汉堡模板中的类,它应该是这样的:

public class SettingsService
{
    public static SettingsService Instance { get; }
    static SettingsService()
    {
        // implement singleton pattern
        Instance = Instance ?? new SettingsService();
    }

    Template10.Services.SettingsService.ISettingsHelper _helper;
    private SettingsService()
    {
        _helper = new Template10.Services.SettingsService.SettingsHelper();
    }

    // add your custom settings here like this:
    public bool SettingName
    {
        get { return _helper.Read(nameof(SettingName), false); }  // 2nd argument is the default value
        set { _helper.Write(nameof(SettingName), value); }
    }
}

如您所见,它实现了单例模式并使用 Template10 中的帮助程序来读取和写入应用程序设置中的值。我还在那里添加了一个名为SettingName.

要在 ViewModel 中使用它,请创建一个私有变量:

private SettingsService _settings = SettingsService.Instance;

然后在任何你想要的方法、getter 或 setter 中使用它,如下所示:

var something = _settings.SettingName;  // read
_settings.SettingName = true;  // write

如果您想根据某些设置更改应用程序的行为,推荐的方法是在SettingsService类的 setter 中进行。但是,我可以想象您将直接在 ViewModel 中进行更改的情况。

于 2016-03-09T15:39:49.040 回答