public async void Save_as(IStorageFile file, Excuse excuseToWrite) { using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) using(Stream outputStream = stream.AsStreamForWrite()) { DataContractSerializer serializer = new DataContractSerializer(typeof(Excuse)); serializer.WriteObject(outputStream, excuseToWrite); } OnPropertyChanged("excuseToWrite"); }
public async Task<Excuse> OpenFile(IStorageFile file, Excuse excuseToRead) { using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) using(Stream inputStream = stream.AsStreamForRead()) { DataContractSerializer serializer = new DataContractSerializer(typeof(Excuse)); excuseToRead = serializer.ReadObject(inputStream) as Excuse; } return excuseToRead; }
private async void randomExcuse_Click(object sender, RoutedEventArgs e) { loading = true; IReadOnlyList<IStorageFile> items = await folder.GetFilesAsync(); List<IStorageFile> files=new List<IStorageFile>(); foreach(IStorageFile item in items) { if (item.FileType == ".xml") files.Add(item); } if (files.Count < 1) currentExcuse = await currentExcuse.OpenFile(files[rand.Next(files.Count)], currentExcuse); else { MessageDialog dialog= new MessageDialog("В папке нету сохраненных извинений"); await dialog.ShowAsync(); } ReBindingData(); }
private async void Open_Click(object sender, RoutedEventArgs e) { loading = true; FileOpenPicker picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.DocumentsLibrary, ViewMode=PickerViewMode.List }; picker.FileTypeFilter.Add(".xml"); file = await picker.PickSingleFileAsync(); string s=file.Path; if (file != null) { try { currentExcuse = await currentExcuse.OpenFile(file, currentExcuse); } catch(SerializationException ex) { MessageDialog dialog = new MessageDialog("Ошибка при чтении файла. Убедитесь что открываемый файл является сохраненным извинением \n"); await dialog.ShowAsync(); } if (currentExcuse.Description != null) ReBindingData(); else return; loading = true; Save.IsEnabled = true; save_as.IsEnabled = true; } }