private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (m_ShouldClose == null) { // Double call DialogHost.Show() throw exceptions // For example - rename sheet and close application. if (m_VM != null && m_VM.DlgHost != null && m_VM.DlgHost.IsOpen) { e.Cancel = true; return; } // mark comment to m_ShouldClose e.Cancel = true; // is here unsaved documents? bool bUnsaved = false; if (m_VM != null && m_VM.CurrentDocument != null) { if (m_VM.CurrentDocument.HasChanges) { bUnsaved = true; } } // if (bUnsaved) { SaveChangesDialog_ViewModel vm = new SaveChangesDialog_ViewModel(); vm.Text = "This document is unsaved and contains some changes. Some data will be lost possibly."; vm.IsSaveButtonVisible = false; SaveChangesDialog saveChangesDialog = new SaveChangesDialog(vm); //show the dialog // true - save // false - cancel // null - continue var result = await DialogHost.Show(saveChangesDialog); if (result is bool && !(bool)result) { return; } } m_ShouldClose = true; Application.Current.Shutdown(); } }
//============================================================================= private Task <object> OnBeforeAppShutdown() { if (m_CurrentDocument == null) { return(null); } // Ask for save before app close. if (m_CurrentDocument.HasChanges) { // undo all not completed changes m_CurrentDocument.SetTheLastState(); SaveChangesDialog_ViewModel saveChangesDialogVM = new SaveChangesDialog_ViewModel(); saveChangesDialogVM.Text = "Application session is limited by 24 hours. Application will be closed. Do you want to save changes?"; saveChangesDialogVM.IsSaveButtonVisible = true; saveChangesDialogVM.IsCancelButtonVisible = false; SaveChangesDialog saveChangesDialog = new SaveChangesDialog(saveChangesDialogVM); // Close previous dialog. if (DlgHost != null && DlgHost.IsOpen) { DlgHost.IsOpen = false; } return(DialogHost.Show(saveChangesDialog).ContinueWith <object>((previousTask) => { var prevTaskResult = previousTask.Result; // true - save // false - cancel // null - continue if (prevTaskResult is bool && (bool)prevTaskResult) { // save the document string strFilePath = string.Empty; if (m_CurrentDocument.IsItNewDocument || m_CurrentDocument.NameWithoutExtension != m_CurrentDocument.CustomerENQ) { string strOldPath = null; if (!m_CurrentDocument.IsItNewDocument && !string.IsNullOrEmpty(m_CurrentDocument.Path)) { strOldPath = m_CurrentDocument.Path; } strFilePath = FileUtils._SaveFileDialog(DrawingDocument.FILE_FILTER, DrawingDocument.FILE_EXTENSION, m_CurrentDocument.CustomerENQ, m_CurrentDocument.CustomerENQ, strOldPath); if (string.IsNullOrEmpty(strFilePath)) { return null; } } // Try to save in UI thread, otherwise DrawingDocument.Save throws an exception on DrawingControl.Update() method which updates UI. if (DrawingControl != null) { DrawingControl.Dispatcher.Invoke(new Action(() => { m_CurrentDocument.Save(strFilePath); } )); return true; } else { return m_CurrentDocument.Save(strFilePath); } } return null; })); } else { return(this.DisplayMessageDialog("Application session is limited by 24 hours. Application will be closed.")); } return(null); }
//============================================================================= public async Task <bool> OpenDrawing(string strDrawingPath) { DrawingDocument.ClearErrors(); if (string.IsNullOrEmpty(strDrawingPath)) { return(false); } if (!File.Exists(strDrawingPath)) { await this.DisplayMessageDialog("Cant open document \"" + strDrawingPath + "\". File doesnt exist."); return(false); } // bool bIsNewDocOpened = false; try { FileStream fs = new FileStream(strDrawingPath, FileMode.Open); if (fs != null) { BinaryFormatter bf = new BinaryFormatter(); DrawingDocument doc = (DrawingDocument)bf.Deserialize(fs); doc.DisplayDialog = this; doc.Path = strDrawingPath; // save old document if new document is not correct and user dont want to fix it DrawingDocument oldDoc = this.CurrentDocument; // this.CurrentDocument = doc; bool bCorrect = await doc.CheckDocument(true, false); // if opened document is not correct then set old document if (!bCorrect) { this.CurrentDocument = oldDoc; } else { doc.MarkStateChanged(); doc.RemoveAllStatesExceptCurrent(); bIsNewDocOpened = true; // Clear changed sheets guids. DrawingSheet.m_ChangedSheetsGuidsSet.Clear(); } // fs.Close(); fs.Dispose(); } } catch { } // if (DrawingDocument._sDontSupportDocument) { // open document which is not supported SaveChangesDialog_ViewModel saveChanges_VM = new SaveChangesDialog_ViewModel(); saveChanges_VM.Text = "This document has very old version, its not supported. Save this document in this application possibly lead to data loss."; saveChanges_VM.IsSaveButtonVisible = false; saveChanges_VM.IsCancelButtonVisible = false; SaveChangesDialog saveChangesDialog = new SaveChangesDialog(saveChanges_VM); //show the dialog // true - save // false - cancel // null - continue var result = await DialogHost.Show(saveChangesDialog); } else if (DrawingDocument._sBiggerMajorNumber > 0) { await this.DisplayMessageDialog("An error occurred while opening document. Document was created in the new version of the application and doesnt supported in this application version.\nDocument was not opened."); return(false); } else if (DrawingDocument._sNewVersion_StreamRead > 0) { // open new document in old version application SaveChangesDialog_ViewModel saveChanges_VM = new SaveChangesDialog_ViewModel(); saveChanges_VM.Text = "This document was created\\saved in the new version of application. It can be drawn or working incorrect. Save this document in this application possibly lead to data loss."; saveChanges_VM.IsSaveButtonVisible = false; saveChanges_VM.IsCancelButtonVisible = false; SaveChangesDialog saveChangesDialog = new SaveChangesDialog(saveChanges_VM); //show the dialog // true - save // false - cancel // null - continue var result = await DialogHost.Show(saveChangesDialog); } else if (DrawingDocument._sStreamReadException > 0) { // SaveChangesDialog_ViewModel saveChanges_VM = new SaveChangesDialog_ViewModel(); saveChanges_VM.Text = "Exceptions ocurred while open document. It can be drawn or working incorrect. Save this document in this application possibly lead to data loss."; saveChanges_VM.IsSaveButtonVisible = false; saveChanges_VM.IsCancelButtonVisible = false; SaveChangesDialog saveChangesDialog = new SaveChangesDialog(saveChanges_VM); //show the dialog // true - save // false - cancel // null - continue var result = await DialogHost.Show(saveChangesDialog); } else { if (bIsNewDocOpened && this.CurrentDocument != null) { // ENQ number is neccessaru for any kind of export. // Display the message if it is empty. if (string.IsNullOrEmpty(this.CurrentDocument.CustomerENQ)) { await this.DisplayMessageDialog("ENQ number is neccessary for any kind of export. Please go to the Customer Info and input ENQ number."); } else { // Check that ENQ number can be used as a filename for the text file. if (this.CurrentDocument.CustomerENQ.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) { await this.DisplayMessageDialog("ENQ number contains incorrect characters for the filename. Please go to the Customer Info and edit ENQ number."); } else if (this.CurrentDocument.NameWithoutExtension != this.CurrentDocument.CustomerENQ) { await this.DisplayMessageDialog("ENQ number is different from the document name. Please use ENQ number as a document name."); } } return(true); } } return(false); }
//============================================================================= private async void CloseSheettButton_Click(object sender, RoutedEventArgs e) { if (m_VM == null || m_VM.CurrentDocument == null) { return; } if (m_VM.CurrentDocument.Sheets.Count == 1) { return; } Button btn = sender as Button; if (btn == null) { return; } ListBoxItem lbi = Utils.TryFindParent <ListBoxItem>(btn); if (lbi == null) { return; } DrawingSheet sheetToClose = lbi.DataContext as DrawingSheet; if (sheetToClose == null) { return; } if (sheetToClose.Rectangles.Count > 0) { StringBuilder sb = new StringBuilder(); sb.Append("Sheet \""); sb.Append(sheetToClose.DisplayName); sb.Append("\" has geometry inside. Do you want to close it? All geometry inside will be deleted with the sheet."); SaveChangesDialog_ViewModel vm = new SaveChangesDialog_ViewModel(); vm.Text = sb.ToString(); vm.IsSaveButtonVisible = false; SaveChangesDialog saveChangesDialog = new SaveChangesDialog(vm); //show the dialog // true - save // false - cancel // null - continue var result = await DialogHost.Show(saveChangesDialog); if (result is bool) { bool bRes = (bool)result; // cancel - dont close document if (!bRes) { return; } } } // select document m_VM.CurrentDocument.RemoveSheet(sheetToClose); m_VM.CurrentDocument.MarkStateChanged(); }