public SettingsServiceBase(SettingsStrategies strategy, string folderName, bool createFolderIfNotExists) { _strategy = strategy; Helper = new SettingsHelper(); switch (strategy) { case SettingsStrategies.Local: _container = ApplicationData.Current.LocalSettings; break; case SettingsStrategies.Roam: _container = ApplicationData.Current.RoamingSettings; break; default: throw new ArgumentException($"Unsupported Settings Strategy: {strategy}", nameof(strategy)); } if (!string.IsNullOrWhiteSpace(folderName)) { try { _container = _container.CreateContainer(folderName, createFolderIfNotExists ? ApplicationDataCreateDisposition.Always : ApplicationDataCreateDisposition.Existing); } catch (Exception) { throw new KeyNotFoundException($"No folder exists named '{folderName}'"); } } }
/// <summary> /// Creates an <c>ISettingsService</c> object targeting the requested (optional) <paramref name="folderName"/> /// in the <paramref name="strategy"/> container. /// </summary> /// <param name="strategy">Roaming or Local</param> /// <param name="folderName">Name of the settings folder to use</param> /// <param name="createFolderIfNotExists"><c>true</c> to create the folder if it isn't already there, false otherwise.</param> /// <returns></returns> public static ISettingsService Create(SettingsStrategies strategy, string folderName = null, bool createFolderIfNotExists = true) { ApplicationDataContainer rootContainer; switch (strategy) { case SettingsStrategies.Local: rootContainer = ApplicationData.Current.LocalSettings; break; case SettingsStrategies.Roam: rootContainer = ApplicationData.Current.RoamingSettings; break; default: throw new ArgumentException($"Unsupported Settings Strategy: {strategy}", nameof(strategy)); } ApplicationDataContainer targetContainer = rootContainer; if (!string.IsNullOrWhiteSpace(folderName)) { try { targetContainer = rootContainer.CreateContainer(folderName, createFolderIfNotExists ? ApplicationDataCreateDisposition.Always : ApplicationDataCreateDisposition.Existing); } catch (Exception) { throw new KeyNotFoundException($"No folder exists named '{folderName}'"); } } return(new SettingsService(targetContainer)); }
public T Read <T>(string key, T otherwise, SettingsStrategies strategy = SettingsStrategies.Local) { if (Windows.ApplicationModel.DesignMode.DesignModeEnabled) { return(otherwise); } return(Container(strategy).Read(key, otherwise)); }
public void Write <T>(string key, T value, SettingsStrategies strategy = SettingsStrategies.Local) { if (Windows.ApplicationModel.DesignMode.DesignModeEnabled) { return; } Container(strategy).Write(key, value); }
public bool Exists(string key, SettingsStrategies strategy = SettingsStrategies.Local) { if (Windows.ApplicationModel.DesignMode.DesignModeEnabled) { return(true); } return(Container(strategy).Exists(key)); }
public void Remove(string key, SettingsStrategies strategy = SettingsStrategies.Local) { if (Windows.ApplicationModel.DesignMode.DesignModeEnabled) { return; } if (Container(strategy).Exists(key)) { Container(strategy).Remove(key); } }
public SettingsService(SettingsStrategies strategy, string folderName = null, bool createFolderIfNotExists = true) : base(strategy, folderName, createFolderIfNotExists) { // empty }
public ISettingsService Container(SettingsStrategies strategy) { return((strategy == SettingsStrategies.Local) ? Local : Roaming); }
public SettingsStorage(SettingsStrategies strategy = SettingsStrategies.Roam) { helper = new SettingsHelper(); LargeSettings = new List <string>(); Strategy = strategy; }
public SettingsService Container(SettingsStrategies strategy) => (strategy == SettingsStrategies.Local) ? Local : Roaming;