сохранение и загрузка контролов

This commit is contained in:
kotcheshir73 2021-04-02 11:59:07 +04:00
parent e17b723899
commit 680823a981
4 changed files with 77 additions and 6 deletions

View File

@ -40,6 +40,8 @@ namespace DesktopTools.Controls
/// </summary>
private event Action<bool> CloseSelectEvent;
private readonly object _lockObject = new();
/// <summary>
/// Объект бизнес-логики для получения данных
/// </summary>
@ -90,8 +92,11 @@ namespace DesktopTools.Controls
DialogHelper.MessageException(ex.Message, "Ошибка при конфигурации");
}
}
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<PageNamesForPaginationModel>().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;
}
/// <summary>
/// Событие смены выбрарнного элемента выпдающего спика (вынесено отдельно, чтобы при настройки конфигурации не вызывать прогрузку)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void ToolStripComboBoxPageNamesSelectedIndexChanged(object sender, EventArgs e) => await LoadListAsync();
/// <summary>
/// Вызов события загрузки данных на datagrid
/// </summary>

View File

@ -13,6 +13,11 @@ namespace DesktopTools.Models
/// </summary>
public ControlOpenMode OpenMode { get; set; }
/// <summary>
/// Не загружать данные
/// </summary>
public bool LazyLoading { get; set; } = false;
/// <summary>
/// Событипе на закрытие контрола списка, если открыт как список
/// </summary>

View File

@ -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);

View File

@ -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<Guid, IControl> _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);
}
}
}
}
/// <summary>
@ -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);
}
}
}