- How can be handled configuration values of an application, allowing the user to possibly change the default values here defined?
Suppose you have defined in the App.config file some parameters assigning a predefined default value for each:<configuration> <appSettings> <add key="LoginType" value="DAILY" /> <add key="MergeAudio" value="TRUE" /> .... </appSettings> </configuration>
Then you can define, for example in a Utility class, the following method: it tries to read the key from the IsolatedStorage;
if it failes, it loads, from the configuration file, the possible default value defined by the application and then it save it in the IsolatedStorage for a quicker further use. In fact, next time that function will be called, it will find that key in the IsolatedStorage and the data will be available in less time!public static string GetAppSettings(string key) { //return (GetAppConfigString("App.config", key)); //return null; String value = String.Empty; if (IsolatedStorageSettings.ApplicationSettings.Contains(key)) { value = IsolatedStorageSettings.ApplicationSettings[key].ToString(); } else { // se non c'è nell'isolated storage: cerca di caricare, con GetAppConfigString, il valore di default (se esiste) da app.config, per poi salvarlo nell'Isolated Storage value = GetAppConfigString("app.config", key); if (!value.Equals(String.Empty)) { SetAppSettings(key, GetAppConfigString("app.config", key)); } } return value; }
Finally here it is the utility function to recover the default value of a key from the configuration file:
private static XElement _xmlConfig; private static Object _xmlConfigSyncLock = new Object(); public static string GetAppConfigString(string configName, string key) { if (_xmlConfig == null) { lock (_xmlConfigSyncLock) { _xmlConfig = XElement.Load(configName); } } XElement element = _xmlConfig.Descendants("appSettings").Descendants("add").FirstOrDefault((el) => el.FirstAttribute.Value == key); if (element != null) { XAttribute xAttribute = element.Attribute("value"); if (xAttribute != null) return xAttribute.Value; } return String.Empty; }
When you need to get the key value from your code, you can simply call
#region App.config keys public const String LoginType = "LoginType"; ... #endregion App.config keys string loginTypeString = Utility.GetAppSettings(Constants.LoginType);
and then when the user change that default value, you can update its value in the IsolatedStorage:
Utility.SetAppSettings(Constants.LoginType, LoginType.DAILY.ToString());
where, always in the Utility class:
public static string SetAppSettings(string key, string value) { IsolatedStorageSettings.ApplicationSettings[key] = value; IsolatedStorageSettings.ApplicationSettings.Save(); return value; }
Note that data written to the IsolatedStorageSettings object hasn’t actually gotten committed to the isolated storage area and it is saved only when the application that uses the class is closed. This can occur when the user closes the Web browser, refreshes a page, or browses away from the page. If you want your application to write to isolated storage immediately, you have to call the Save method in application code.
Categorie
Supporta questo Blog
Translate
-
Articoli recenti
- Ricette facili facili: le pere cotte al forno ;-) 26 febbraio 2021
- Ricette facili facili: insalata di polpo ;-) 26 febbraio 2021
- Acquisisci schermata web: come catturare del contenuto di una pagina di un sito web anche quando risiede su parti che richiedono uno scroll (verticale/orizzontale) per poterlo visualizzare interamente 22 febbraio 2021
- Alexa: come continuare ad ascoltare Radio1, Radio2 e Radio3 della RAI … e alcune considerazioni su skill interessanti! 19 febbraio 2021
- Ricette facili facili: le lasagne al forno ;-) 16 febbraio 2021
Articoli in evidenza
Archivi
Classifica Post
- Configurare manualmente un ripetitore TP-LINK recente (e.g. TL-WA850RE vers. 2.x, AC1750) da app o da browser, per estendere la portata del proprio Wi-Fi ... e soluzioni alternative (e.g. reti Mesh, FWA)
- Come connettere uno smartphone o un tablet con un cavo Ethernet
- 18app: come fare se non si riesce più ad autorizzare un accesso con PosteID
- Frattura biossea (tibia e perone) scomposta della gamba: istruzioni per l'uso
- ECOCERT: come richiederlo dal sito dell'INPS e come interpretare la colonna relativa ai contributi settimanali utili ai fini pensionistici ... o anche solo come conoscere approssimativamente i contributi versati!
- Per quanti anni si devono conservare i 730, ricevute fiscali, Tarsu, Imu, multe ecc... ?
- Telecamera IP di sicurezza Xiaomi Mi Home e configurazione per un salvataggio dei video anche su un hard disk esterno collegato alla rete locale
- Drawing UML 2.5 diagrams with Visio 2016 (even with the Standard edition)
- Come avere, a poco prezzo, una connessione a Internet in una seconda casa (e.g. per domotica) senza necessità d'installare una linea fissa
- How to share photos, videos & album with Amazon Photo included in an Amazon Prime subscription
Foto su Flickr
Annunci