Exemple #1
0
        private static async Task <T> LoadSettingsAsync <T>(string fileName, bool usePublisherFolder, Func <T> defaultValue = null)
        {
            StorageFolder baseFolder = usePublisherFolder ? BaseSharedFolder : LocalFolder;

            var sets = (StorageFile)await baseFolder.TryGetItemAsync(fileName);

            if (sets == null && usePublisherFolder)
            {
                sets = (StorageFile)await LocalFolder.TryGetItemAsync(fileName);

                // Moving the file back to the publisher folder (as we wanted to use it)
                if (sets != null)
                {
                    sets = await sets.CopyAsync(baseFolder);
                }
            }

            if (sets != null)
            {
                try
                {
                    var data = await FileIO.ReadTextAsync(sets);

                    return(JsonConvert.DeserializeObject <T>(data));
                }
                catch (Exception e)
                {
                    await sets.CopyAsync(baseFolder, fileName + ".bk", NameCollisionOption.ReplaceExisting);

                    throw e;
                }
            }

            return(defaultValue != null?defaultValue() : Activator.CreateInstance <T>());
        }
Exemple #2
0
        /*Recipe Save*/
        private async void SaveRecipe_Click(object sender, RoutedEventArgs e)
        {   //Save file, prompt for overwrite if it already exists
            try
            {
                StorageFile file;
                if (await LocalFolder.TryGetItemAsync(CurrentFilename) is StorageFile alreadyExists)
                {
                    if (!await ShowConfirmation($"Are you sure you want to overwrite \"{CurrentFilename}\"?"))
                    {
                        return;
                    }
                    file = alreadyExists;
                }
                else
                {
                    file = await LocalFolder.CreateFileAsync(CurrentFilename);

                    RecipeFiles.Add(CurrentFilename.Remove(CurrentFilename.Length - RecipeFilenameExtension.Length));
                }
                using (var stream = await file.OpenStreamForWriteAsync())
                    RecipeSerializer.Serialize(stream, Recipe);
            }
            catch (Exception x)
            {
                await ShowError(x.Message);
            }
        }