private IEnumerable <SqlConnectionViewModel> ConnectionsLoadFromStorage() { var vmcoll = new List <SqlConnectionViewModel>(); Settings.ApplicationSettings appSettings = null; try { using (var store = IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly | IsolatedStorageScope.User, null, null)) { using (var file = store.OpenFile(storageFileName, FileMode.Open, FileAccess.Read)) { var ser = new XmlSerializer(typeof(Settings.ApplicationSettings)); appSettings = (Settings.ApplicationSettings)ser.Deserialize(file); } } // guarantee that we have at least ONE connection, even if it's fake if (appSettings.Connections == null || appSettings.Connections.Length == 0) { var placeholderConnections = new SqlConnectionDto[] { new SqlConnectionDto { ServerAndInstance = "SERVER" } }; appSettings.Connections = placeholderConnections; } foreach (var item in appSettings.Connections) { SqlConnectionViewModel vm = IoC.Get <SqlConnectionViewModel>(); AutoMapper.Mapper.Map(item, vm); vmcoll.Add(vm); } // make sure the stuff handling the SQL Code knows which font to use var fam = new FontFamily(appSettings.FontFamilyName); eventAggregator.PublishOnCurrentThread(fam); } catch (Exception e) { eventAggregator.PublishOnCurrentThread(new ShellMessage { MessageText = e.ToString(), Severity = Severity.Warning }); vmcoll.Clear(); var newvm = IoC.Get <SqlConnectionViewModel>(); newvm.ServerAndInstance = "SERVER"; vmcoll.Add(newvm); } return(vmcoll); }
public void ConnectionsSaveToStorage() { try { var storageScope = IsolatedStorageScope.Assembly | IsolatedStorageScope.User; using (var store = IsolatedStorageFile.GetStore(storageScope, null, null)) { if (store.FileExists(storageFileName)) { store.DeleteFile(storageFileName); } // make sure at least one conx is in the collection if (Items.Count == 0) { var newvm = IoC.Get <SqlConnectionViewModel>(); newvm.ServerAndInstance = "SERVER"; Items.Add(newvm); } using (var file = store.OpenFile(storageFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { XmlSerializer ser = new XmlSerializer(typeof(Settings.ApplicationSettings)); var settings = new Settings.ApplicationSettings(); var ff = this.font ?? new FontFamily("Consolas"); settings.FontFamilyName = ff.Source; settings.Connections = Items.Select(i => AutoMapper.Mapper.Map(i, new SqlConnectionDto())).ToArray(); ser.Serialize(file, settings); } } } catch (Exception e) { eventAggregator.PublishOnCurrentThread(new ShellMessage { MessageText = e.ToString(), Severity = Severity.Warning }); } }