/
hlebusheck
/
notes
Обзор
Документация
Войти
/
hlebusheck
/
notes
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
Notes.Wpf/ConfigurationWindow.xaml.cs
98 строк
3 KB
Hlebusheck
Исправлены вызовы асинхронных функций
20 апр 2024, 16:12
20 апр 2024, 16:12
f6f8238
Код
Авторство
О чём код?
using Microsoft.Win32; using Notes.Model; using Notes.Repository; using Notes.SqliteRepository; using Notes.Wpf.Controls; using System; using System.IO; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace Notes { public partial class ConfigurationWindow : Window { private readonly IMemoRepository _repository; public ConfigurationWindow(IMemoRepository repository) { InitializeComponent(); _repository = repository; DbContext = (DataContext?)App.ServiceProvider.GetService(typeof(DataContext)) ?? throw new Exception("Db is null."); DataContext = this; } public DataContext DbContext { get; } private async void ImportClick(object sender, RoutedEventArgs e) => await Import(); private async void ExportClick(object sender, RoutedEventArgs e) => await Export(); private async Task Import() { var openFileDialog = new OpenFileDialog() { Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*", RestoreDirectory = true }; if (openFileDialog.ShowDialog() == true) { using var stream = File.OpenRead(openFileDialog.FileName); if (stream == null) return; Memo[]? newItems = await JsonSerializer.DeserializeAsync<Memo[]>(stream); stream.Close(); if (newItems == null || newItems.Length == 0) return; await _repository.Import(newItems); if (Owner is MainWindow mainWindow) mainWindow.Refresh(); CustomMessageBox.Show( this, $"Import completed.", "Informing", "\u0027", (SolidColorBrush)Application.Current.FindResource("InformationSolidBrush"), MessageBoxButton.OK); } } private async Task Export() { var saveFileDialog = new SaveFileDialog() { Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*", RestoreDirectory = true, FileName = "Exported" }; if (saveFileDialog.ShowDialog() == true) { var content = JsonSerializer.Serialize((await _repository.Get()).ToArray()); await File.WriteAllTextAsync(saveFileDialog.FileName, content); CustomMessageBox.Show( this, $"Export completed.", "Informing", "\u0030", (SolidColorBrush)Application.Current.FindResource("InformationSolidBrush"), MessageBoxButton.OK); } } private void LoadedWindow(object sender, RoutedEventArgs e) { var minButton = (Button)Template.FindName("MinButton", this); var maxButton = (Button)Template.FindName("MaxButton", this); minButton.Visibility = Visibility.Collapsed; maxButton.Visibility = Visibility.Collapsed; ResizeMode = ResizeMode.NoResize; } } }