public static async Task <AppState> LoadAppStateInitial() { var file = await TryLoadConfigFile().ConfigureAwait(false); AppState resultState = new AppState(); // app state no yet stored if (file == null) { await StoreInitialAppState(resultState); } else { try { resultState = await XMLStorage.ReadObjectFromXmlFileAsync <AppState>(file).ConfigureAwait(false); } catch (Exception ex) { // try to overwrite inconsistent state or crash await StoreInitialAppState(resultState); } } return(resultState); }
internal static async Task <AppState> LoadAppStateFromUserDefinedLocation() { AppState resultState = null; var loadConfigPicker = new FileOpenPicker(); loadConfigPicker.ViewMode = PickerViewMode.List; loadConfigPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; loadConfigPicker.FileTypeFilter.Add(".xml"); StorageFile file = await loadConfigPicker.PickSingleFileAsync(); if (file != null) { var token = StorageApplicationPermissions.FutureAccessList.Add(file, _userDefinedSettingsToken); LocalSettings.Values[_userDefinedSettingsToken] = token; resultState = await XMLStorage.ReadObjectFromXmlFileAsync <AppState>(file); } else { Debug.WriteLine("Loading file was cancelled."); } return(resultState); }
internal static async Task SaveAppStateToUserDefinedLocation(AppState appState) { var savePicker = new Windows.Storage.Pickers.FileSavePicker(); savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; savePicker.FileTypeChoices.Add("Plain Text", new List <string>() { ".xml" }); savePicker.SuggestedFileName = "HomeAutomationSettings"; Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { var token = StorageApplicationPermissions.FutureAccessList.Add(file, _userDefinedSettingsToken); LocalSettings.Values[_userDefinedSettingsToken] = token; // Prevent updates to the remote version of the file until // we finish making changes and call CompleteUpdatesAsync. //Windows.Storage.CachedFileManager.DeferUpdates(file); crashes when used with dropbox // write to file await XMLStorage.SaveObjectToXmlByFile(appState, file); // Let Windows know that we're finished changing the file so // the other app can update the remote version of the file. // Completing updates may require Windows to ask for user input. Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file); if (status == Windows.Storage.Provider.FileUpdateStatus.Complete) { Debug.WriteLine("File " + file.Name + " was saved."); } else { Debug.WriteLine("File " + file.Name + " couldn't be saved."); } } else { Debug.WriteLine("Saving file was cancelled."); } }
public static async Task StoreAppState(AppState state) { var configFile = await TryLoadConfigFile(); await XMLStorage.SaveObjectToXmlByFile(state, configFile); }
private static async Task StoreInitialAppState(AppState appState) { await XMLStorage.SaveObjectToXmlByFileName(appState, _settingsFilename).ConfigureAwait(false); }