Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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.");
            }
        }
Exemplo n.º 4
0
        public static async Task StoreAppState(AppState state)
        {
            var configFile = await TryLoadConfigFile();

            await XMLStorage.SaveObjectToXmlByFile(state, configFile);
        }
Exemplo n.º 5
0
 private static async Task StoreInitialAppState(AppState appState)
 {
     await XMLStorage.SaveObjectToXmlByFileName(appState, _settingsFilename).ConfigureAwait(false);
 }