From 680823a981552d8ac5c79e1a27f1138c1c376873 Mon Sep 17 00:00:00 2001 From: kotcheshir73 Date: Fri, 2 Apr 2021 11:59:07 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B8=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83?= =?UTF-8?q?=D0=B7=D0=BA=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MainControls/GenericControlEntityList.cs | 31 +++++++++++-- .../DesktopTools/Models/ControlOpenModel.cs | 5 ++ .../FormMain.Designer.cs | 1 + .../DepartmentPortalDesctop/FormMain.cs | 46 ++++++++++++++++++- 4 files changed, 77 insertions(+), 6 deletions(-) diff --git a/DepartmentPortal/Common/DesktopTools/MainControls/GenericControlEntityList.cs b/DepartmentPortal/Common/DesktopTools/MainControls/GenericControlEntityList.cs index 016a3b8..c6e6c2f 100644 --- a/DepartmentPortal/Common/DesktopTools/MainControls/GenericControlEntityList.cs +++ b/DepartmentPortal/Common/DesktopTools/MainControls/GenericControlEntityList.cs @@ -40,6 +40,8 @@ namespace DesktopTools.Controls /// private event Action CloseSelectEvent; + private readonly object _lockObject = new(); + /// /// Объект бизнес-логики для получения данных /// @@ -90,7 +92,10 @@ namespace DesktopTools.Controls DialogHelper.MessageException(ex.Message, "Ошибка при конфигурации"); } } - await LoadListAsync(); + if (!model.LazyLoading) + { + await LoadListAsync(); + } } public IControl GetInstanceControl() => _genericControlViewEntityList?.GetInstanceGenericControl(); @@ -110,9 +115,9 @@ namespace DesktopTools.Controls new XElement("Value", ((toolStripComboBoxPageNames.SelectedItem as PageNamesForPaginationModel)?.Value ?? "-"))), new XElement("ParentId", ParentId?.ToString() ?? "-"))).ToString(); - public void LoadControlFromXml(string xml) + public async void LoadControlFromXml(string xml) { - var control = XElement.Parse(xml).Element("Control"); + var control = XElement.Parse(xml); ControlId = new Guid(control.Attribute("ControlId").Value.ToString()); Title = control.Attribute("Title").Value.ToString(); AccessOperation = (AccessOperation)Enum.Parse(typeof(AccessOperation), control.Attribute("AccessOperation").Value.ToString()); @@ -126,12 +131,19 @@ namespace DesktopTools.Controls Key = config.Element("PageName").Element("Key").Value.ToString(), Value = config.Element("PageName").Element("Value").Value.ToString() }; - toolStripComboBoxPageNames.SelectedItem = pageName; + var elem = toolStripComboBoxPageNames.Items.Cast().FirstOrDefault(x => x.Key == pageName.Key); + if (elem != null) + { + toolStripComboBoxPageNames.SelectedIndexChanged -= ToolStripComboBoxPageNamesSelectedIndexChanged; + toolStripComboBoxPageNames.SelectedItem = elem; + toolStripComboBoxPageNames.SelectedIndexChanged += ToolStripComboBoxPageNamesSelectedIndexChanged; + } } if (config.Element("ParentId").Value.ToString() != "-") { ParentId = new Guid(config.Element("ParentId").Value.ToString()); } + await LoadListAsync(); } #endregion @@ -233,7 +245,9 @@ namespace DesktopTools.Controls toolStripLabelCountRecords.Visible = toolStripTextBoxCountRecords.Visible = toolStripButtonNext.Visible = false; toolStripComboBoxPageNames.Items.AddRange(config.PageNamesForPagination.ToArray()); + toolStripComboBoxPageNames.SelectedIndexChanged -= ToolStripComboBoxPageNamesSelectedIndexChanged; toolStripComboBoxPageNames.SelectedIndex = 0; + toolStripComboBoxPageNames.SelectedIndexChanged += ToolStripComboBoxPageNamesSelectedIndexChanged; toolStripComboBoxPageNames.Tag = config.ParentPropertyName; } } @@ -332,9 +346,16 @@ namespace DesktopTools.Controls } }; - toolStripComboBoxPageNames.SelectedIndexChanged += async (object sender, EventArgs e) => { await LoadListAsync(); }; + toolStripComboBoxPageNames.SelectedIndexChanged += ToolStripComboBoxPageNamesSelectedIndexChanged; } + /// + /// Событие смены выбрарнного элемента выпдающего спика (вынесено отдельно, чтобы при настройки конфигурации не вызывать прогрузку) + /// + /// + /// + private async void ToolStripComboBoxPageNamesSelectedIndexChanged(object sender, EventArgs e) => await LoadListAsync(); + /// /// Вызов события загрузки данных на datagrid /// diff --git a/DepartmentPortal/Common/DesktopTools/Models/ControlOpenModel.cs b/DepartmentPortal/Common/DesktopTools/Models/ControlOpenModel.cs index 810fe2f..33e3285 100644 --- a/DepartmentPortal/Common/DesktopTools/Models/ControlOpenModel.cs +++ b/DepartmentPortal/Common/DesktopTools/Models/ControlOpenModel.cs @@ -13,6 +13,11 @@ namespace DesktopTools.Models /// public ControlOpenMode OpenMode { get; set; } + /// + /// Не загружать данные + /// + public bool LazyLoading { get; set; } = false; + /// /// Событипе на закрытие контрола списка, если открыт как список /// diff --git a/DepartmentPortal/DepartmentPortalDesctop/FormMain.Designer.cs b/DepartmentPortal/DepartmentPortalDesctop/FormMain.Designer.cs index 18b6b38..0106e8e 100644 --- a/DepartmentPortal/DepartmentPortalDesctop/FormMain.Designer.cs +++ b/DepartmentPortal/DepartmentPortalDesctop/FormMain.Designer.cs @@ -128,6 +128,7 @@ namespace DepartmentPortalDesctop this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Кафедральный портал"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormMain_FormClosing); this.panelControls.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridViewControls)).EndInit(); this.ResumeLayout(false); diff --git a/DepartmentPortal/DepartmentPortalDesctop/FormMain.cs b/DepartmentPortal/DepartmentPortalDesctop/FormMain.cs index bc107e1..f3c4b61 100644 --- a/DepartmentPortal/DepartmentPortalDesctop/FormMain.cs +++ b/DepartmentPortal/DepartmentPortalDesctop/FormMain.cs @@ -1,12 +1,13 @@ using DesktopTools.BusinessLogics; -using DesktopTools.Controls; using DesktopTools.Enums; using DesktopTools.Interfaces; using DesktopTools.Models; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Windows.Forms; +using System.Xml.Linq; namespace DepartmentPortalDesctop { @@ -16,6 +17,8 @@ namespace DepartmentPortalDesctop private readonly Dictionary _controls; + private readonly string _fileForXmlConfig = "ControlsConfig.xml"; + public FormMain() { InitializeComponent(); @@ -51,6 +54,33 @@ namespace DepartmentPortalDesctop menuMain.Items.Add(menu); } } + + if(File.Exists(_fileForXmlConfig)) + { + var xml = XDocument.Load(_fileForXmlConfig).Element("Controls"); + Guid? id = null; + if (xml != null) + { + foreach(XElement node in xml.Elements("Control")) + { + var type = node.Attribute("Type").Value.ToString(); + var control = _baseControls.Values.FirstOrDefault(x => x.GetType().FullName == type); + if (control != null) + { + var ctrl = control.GetInstance(); + ctrl.Open(new ControlOpenModel { OpenMode = ControlOpenMode.List, LazyLoading = true, CloseList = CloseControl }); + ctrl.LoadFromXml(node.ToString()); + _controls.Add(ctrl.ControlId, ctrl); + dataGridViewControls.Rows.Insert(0, new object[] { ctrl.ControlId, ctrl.Title }); + id = ctrl.ControlId; + } + } + if (id.HasValue) + { + ShowControl(id.Value); + } + } + } } /// @@ -143,5 +173,19 @@ namespace DepartmentPortalDesctop } private void DataGridViewControls_CellClick(object sender, DataGridViewCellEventArgs e) => ShowControl(new Guid(dataGridViewControls.Rows[e.RowIndex].Cells[0].Value.ToString())); + + private void FormMain_FormClosing(object sender, FormClosingEventArgs e) + { + var xml = new XElement("Controls"); + foreach(var cntrl in _controls) + { + xml.AddFirst(XElement.Parse(cntrl.Value.SaveToXml())); + } + if (File.Exists(_fileForXmlConfig)) + { + File.Delete(_fileForXmlConfig); + } + xml.Save(_fileForXmlConfig); + } } } \ No newline at end of file