From 012cbc4d6d2f4602cbd67c0050bf3e755b2daf09 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Thu, 11 Sep 2025 09:18:15 +0200 Subject: [PATCH] Fix save and restore state --- ENI2/Controls/EasyPeasyControl.xaml.cs | 82 +++++++++++++++----------- ENI2/MainWindow.xaml.cs | 2 + 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/ENI2/Controls/EasyPeasyControl.xaml.cs b/ENI2/Controls/EasyPeasyControl.xaml.cs index 0d1cbabf..f672e70e 100644 --- a/ENI2/Controls/EasyPeasyControl.xaml.cs +++ b/ENI2/Controls/EasyPeasyControl.xaml.cs @@ -4,23 +4,13 @@ using bsmd.database.EasyPeasy; using ENI2.Util; -using Microsoft.Office.Interop.Excel; using Microsoft.Win32; using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; using System.Xml; using System.Xml.Serialization; @@ -39,18 +29,16 @@ namespace ENI2.Controls InitializeComponent(); } - private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e) + public void SaveState() { - if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) + try { - if (Clipboard.ContainsText()) - { - var text = Clipboard.GetText(); - PasteGoodsItems(text); - e.Handled = true; - } + EasyPeasyState.Save(_vm); } - } + catch { } + } + + #region button event handler private void buttonClear_Click(object sender, RoutedEventArgs e) { @@ -98,6 +86,28 @@ namespace ENI2.Controls } } + private void buttonImport_Click(object sender, RoutedEventArgs e) + { + OpenFileDialog ofd = new OpenFileDialog(); + ofd.Filter = "XML file|*.xml"; + ofd.RestoreDirectory = true; + ofd.Multiselect = false; + + if (ofd.ShowDialog() == true) + { + using (var fs = File.OpenRead(ofd.FileName)) + { + var ser = new XmlSerializer(typeof(ProofRequest)); + _vm = (ProofRequest)ser.Deserialize(fs); + this.DataContext = _vm; + } + } + } + + #endregion + + #region loaded/unloaded event handler + private void UserControl_Loaded(object sender, RoutedEventArgs e) { _vm = EasyPeasyState.LoadOrCreate(); @@ -121,6 +131,23 @@ namespace ENI2.Controls catch { } } + #endregion + + #region cut & paste logic + + private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e) + { + if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) + { + if (Clipboard.ContainsText()) + { + var text = Clipboard.GetText(); + PasteGoodsItems(text); + e.Handled = true; + } + } + } + private void PasteGoodsItems(string text) { if (_vm?.ProofInformationT2LT2LF?.GoodsShipmentForT2LT2LF == null) return; @@ -173,23 +200,8 @@ namespace ENI2.Controls return res.ToArray(); } - private void buttonImport_Click(object sender, RoutedEventArgs e) - { - OpenFileDialog ofd = new OpenFileDialog(); - ofd.Filter = "XML file|*.xml"; - ofd.RestoreDirectory = true; - ofd.Multiselect = false; + #endregion - if (ofd.ShowDialog() == true) - { - using (var fs = File.OpenRead(ofd.FileName)) - { - var ser = new XmlSerializer(typeof(ProofRequest)); - _vm = (ProofRequest)ser.Deserialize(fs); - this.DataContext = _vm; - } - } - } } } diff --git a/ENI2/MainWindow.xaml.cs b/ENI2/MainWindow.xaml.cs index 075c0431..d8d4cd0f 100644 --- a/ENI2/MainWindow.xaml.cs +++ b/ENI2/MainWindow.xaml.cs @@ -386,6 +386,8 @@ namespace ENI2 Properties.Settings.Default.MainWindowPlacement = this.GetPlacement(); Properties.Settings.Default.Save(); Microsoft.Win32.SystemEvents.SessionEnded -= SystemEvents_SessionEnded; + if (easyPeasyControl != null) + easyPeasyControl.SaveState(); } private void Window_SourceInitialized(object sender, EventArgs e)