Методы загрузки и выгрузки xml

This commit is contained in:
kotcheshir73 2021-04-02 09:29:09 +04:00
parent ed483159e8
commit 2ce5f9b653
10 changed files with 135 additions and 39 deletions

View File

@ -35,5 +35,17 @@ namespace DesktopTools.Interfaces
/// </summary>
/// <param name="model"></param>
void Open(ControlOpenModel model);
/// <summary>
/// Сохранение настроек в xml-строку
/// </summary>
/// <returns></returns>
string SaveToXml();
/// <summary>
/// Загрузка настроек из xml-строки
/// </summary>
/// <param name="xml"></param>
void LoadFromXml(string xml);
}
}

View File

@ -18,5 +18,17 @@ namespace DesktopTools.Interfaces
/// </summary>
/// <returns></returns>
IControl GetInstanceControl();
/// <summary>
/// Сохранение настроек в xml-строку
/// </summary>
/// <returns></returns>
string SaveControlToXml();
/// <summary>
/// Загрузка настроек из xml-строки
/// </summary>
/// <param name="xml"></param>
void LoadControlFromXml(string xml);
}
}

View File

@ -25,5 +25,17 @@ namespace DesktopTools.Interfaces
/// </summary>
/// <param name="id"></param>
string GetTitleFromIdControl(Guid id);
/// <summary>
/// Сохранение настроек в xml-строку
/// </summary>
/// <returns></returns>
string SaveControlToXml();
/// <summary>
/// Загрузка настроек из xml-строки
/// </summary>
/// <param name="xml"></param>
void LoadControlFromXml(string xml);
}
}

View File

@ -13,6 +13,7 @@ using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml.Linq;
namespace DesktopTools.Controls
{
@ -50,14 +51,29 @@ namespace DesktopTools.Controls
/// </summary>
private event Action<Guid> CloseElementEvent;
/// <summary>
/// События установки одинаковой ширины для заголовков контролов
/// </summary>
private event Action<int> SetTitleWidth;
/// <summary>
/// Событие установки значения
/// </summary>
private event Action<object> SetValues;
/// <summary>
/// Событие сброса значения в исходное состояние
/// </summary>
private event Action DropValues;
/// <summary>
/// Событие проверки заполненности контрола
/// </summary>
private event Func<bool> CheckValues;
/// <summary>
/// Событие получения значения из контрола
/// </summary>
private event Action<object> GetValues;
private E Element
@ -79,7 +95,7 @@ namespace DesktopTools.Controls
}
if (page.Controls[0] is IControlChildEntity cntrl)
{
cntrl.ParentObject = _element;
cntrl.ParentId = _element.Id;
cntrl.Open(new ControlOpenModel { OpenMode = ControlOpenMode.Child });
}
}
@ -129,6 +145,20 @@ namespace DesktopTools.Controls
Dock = DockStyle.Fill;
}
public IControl GetInstanceControl() => _genericControlViewEntityElement?.GetInstanceGenericControl();
public string SaveControlToXml() => new XElement("Control",
new XAttribute("Type", GetType().FullName),
new XAttribute("ControlId", ControlId),
new XAttribute("Title", Title)).ToString();
public void LoadControlFromXml(string xml)
{
var control = XElement.Parse(xml).Element("Control");
ControlId = new Guid(control.Attribute("ControlId").Value.ToString());
Title = control.Attribute("Title").Value.ToString();
}
private void InitEvents()
{
toolStripButtonSave.Click += (object sender, EventArgs e) => { Save(); };
@ -295,7 +325,7 @@ namespace DesktopTools.Controls
}
}
public bool Save()
private bool Save()
{
if (CheckValues.GetInvocationList().Select(x => (bool)x.DynamicInvoke()).ToList().Any(x => !x))
{
@ -334,7 +364,5 @@ namespace DesktopTools.Controls
return false;
}
public IControl GetInstanceControl() => _genericControlViewEntityElement?.GetInstanceGenericControl();
}
}

View File

@ -5,6 +5,7 @@ using DesktopTools.Models;
using ModuleTools.Attributes;
using ModuleTools.BindingModels;
using ModuleTools.BusinessLogics;
using ModuleTools.Enums;
using ModuleTools.Extensions;
using ModuleTools.ViewModels;
using System;
@ -13,6 +14,7 @@ using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace DesktopTools.Controls
{
@ -94,6 +96,43 @@ namespace DesktopTools.Controls
public IControl GetInstanceControl() => _genericControlViewEntityList?.GetInstanceGenericControl();
public string GetTitleFromIdControl(Guid id) => _businessLogic.GetElement(new G { Id = id })?.ToString();
public string SaveControlToXml() => new XElement("Control",
new XAttribute("Type", GetType().FullName),
new XAttribute("ControlId", ControlId),
new XAttribute("Title", Title),
new XAttribute("AccessOperation", AccessOperation),
new XElement("Configurate",
new XElement("Page", toolStripTextBoxPage.Text),
new XElement("CountElementsOnPage", toolStripTextBoxCountRecords.Text),
new XElement("PageName",
new XElement("Key", ((toolStripComboBoxPageNames.SelectedItem as PageNamesForPaginationModel)?.Key ?? "-")),
new XElement("Value", ((toolStripComboBoxPageNames.SelectedItem as PageNamesForPaginationModel)?.Value ?? "-"))),
new XElement("ParentId", ParentId?.ToString() ?? "-"))).ToString();
public void LoadControlFromXml(string xml)
{
var control = XElement.Parse(xml).Element("Control");
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());
var config = control.Element("Configurate");
toolStripTextBoxPage.Text = config.Element("Page").Value.ToString();
toolStripTextBoxCountRecords.Text = config.Element("CountElementsOnPage").Value.ToString();
if (config.Element("PageName").Element("Key").Value.ToString() != "-")
{
var pageName = new PageNamesForPaginationModel
{
Key = config.Element("PageName").Element("Key").Value.ToString(),
Value = config.Element("PageName").Element("Value").Value.ToString()
};
toolStripComboBoxPageNames.SelectedItem = pageName;
}
if (config.Element("ParentId").Value.ToString() != "-")
{
ParentId = new Guid(config.Element("ParentId").Value.ToString());
}
}
#endregion
/// <summary>
@ -330,13 +369,9 @@ namespace DesktopTools.Controls
if (ParentPropertyName.IsNotEmpty())
{
if (ParentObject != null)
if (ParentId.HasValue)
{
var property = ParentObject.GetType().GetProperty("Id");
if (property != null)
{
await Task.Run(() => data = GetDataFromParent(new Guid(property.GetValue(ParentObject).ToString())));
}
await Task.Run(() => data = GetDataFromParent(ParentId.Value));
Visible = true;
}
else
@ -495,10 +530,9 @@ namespace DesktopTools.Controls
try
{
var control = ControlViewEntityElement.GetInstance() as IControlChildEntity;
var property = ParentObject?.GetType()?.GetProperty("Id");
if (property != null)
if (ParentId.HasValue)
{
control.ParentId = new Guid(property.GetValue(ParentObject).ToString());
control.ParentId = ParentId;
control.ParentPropertyName = ParentPropertyName;
}
if (toolStripFooter.Visible && toolStripComboBoxPageNames.Visible)

View File

@ -24,6 +24,10 @@ namespace DesktopTools.Controls
public IControl GetInstance() => _controlViewEntityElement?.GetInstanceControl();
public string SaveToXml() => _controlViewEntityElement?.SaveControlToXml();
public void LoadFromXml(string xml) => _controlViewEntityElement?.LoadControlFromXml(xml);
#endregion
#region IControlChildEntity

View File

@ -23,6 +23,10 @@ namespace DesktopTools.Controls
public void Open(ControlOpenModel model) => _controlViewEntityList?.OpenControl(model);
public IControl GetInstance() => _controlViewEntityList?.GetInstanceControl();
public string SaveToXml() => _controlViewEntityList?.SaveControlToXml();
public void LoadFromXml(string xml) => _controlViewEntityList?.LoadControlFromXml(xml);
#endregion
#region IControlEntityList

View File

@ -34,12 +34,10 @@ namespace DepartmentPortalDesctop
this.dataGridViewControls = new System.Windows.Forms.DataGridView();
this.ColumnControlId = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ColumnControlName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.splitContainerMain = new System.Windows.Forms.SplitContainer();
this.ButtonShowHideControlList = new System.Windows.Forms.Button();
this.panelControl = new System.Windows.Forms.Panel();
this.panelControls.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridViewControls)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.splitContainerMain)).BeginInit();
this.splitContainerMain.SuspendLayout();
this.SuspendLayout();
//
// menuMain
@ -97,20 +95,6 @@ namespace DepartmentPortalDesctop
this.ColumnControlName.Name = "ColumnControlName";
this.ColumnControlName.ReadOnly = true;
//
// splitContainerMain
//
this.splitContainerMain.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainerMain.Location = new System.Drawing.Point(216, 24);
this.splitContainerMain.Name = "splitContainerMain";
this.splitContainerMain.Panel1MinSize = 100;
//
// splitContainerMain.Panel2
//
this.splitContainerMain.Panel2.AutoScroll = true;
this.splitContainerMain.Size = new System.Drawing.Size(584, 426);
this.splitContainerMain.SplitterDistance = 295;
this.splitContainerMain.TabIndex = 2;
//
// ButtonShowHideControlList
//
this.ButtonShowHideControlList.Dock = System.Windows.Forms.DockStyle.Left;
@ -122,12 +106,20 @@ namespace DepartmentPortalDesctop
this.ButtonShowHideControlList.UseVisualStyleBackColor = true;
this.ButtonShowHideControlList.Click += new System.EventHandler(this.ButtonShowHideControlList_Click);
//
// panelControl
//
this.panelControl.Dock = System.Windows.Forms.DockStyle.Fill;
this.panelControl.Location = new System.Drawing.Point(216, 24);
this.panelControl.Name = "panelControl";
this.panelControl.Size = new System.Drawing.Size(584, 426);
this.panelControl.TabIndex = 4;
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.splitContainerMain);
this.Controls.Add(this.panelControl);
this.Controls.Add(this.ButtonShowHideControlList);
this.Controls.Add(this.panelControls);
this.Controls.Add(this.menuMain);
@ -138,8 +130,6 @@ namespace DepartmentPortalDesctop
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.panelControls.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridViewControls)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.splitContainerMain)).EndInit();
this.splitContainerMain.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
@ -149,11 +139,11 @@ namespace DepartmentPortalDesctop
private System.Windows.Forms.MenuStrip menuMain;
private System.Windows.Forms.Panel panelControls;
private System.Windows.Forms.SplitContainer splitContainerMain;
private System.Windows.Forms.Button ButtonShowHideControlList;
private System.Windows.Forms.DataGridView dataGridViewControls;
private System.Windows.Forms.DataGridViewTextBoxColumn ColumnControlId;
private System.Windows.Forms.DataGridViewTextBoxColumn ColumnControlName;
private System.Windows.Forms.Panel panelControl;
}
}

View File

@ -74,8 +74,8 @@ namespace DepartmentPortalDesctop
ctrl.Open(new ControlOpenModel { OpenMode = ControlOpenMode.List, CloseList = CloseControl });
_controls.Add(ctrl.ControlId, ctrl);
splitContainerMain.Panel1.Controls.Clear();
splitContainerMain.Panel1.Controls.Add(ctrl as UserControl);
panelControl.Controls.Clear();
panelControl.Controls.Add(ctrl as UserControl);
dataGridViewControls.Rows.Add(new object[] { ctrl.ControlId, ctrl.Title });
dataGridViewControls.Rows[^1].Selected = true;
}
@ -132,8 +132,8 @@ namespace DepartmentPortalDesctop
{
return;
}
splitContainerMain.Panel1.Controls.Clear();
splitContainerMain.Panel1.Controls.Add(ctrl as UserControl);
panelControl.Controls.Clear();
panelControl.Controls.Add(ctrl as UserControl);
}
private void ButtonShowHideControlList_Click(object sender, EventArgs e)

View File

@ -28,7 +28,7 @@ namespace DepartmentPortalDesctop
if (form.ShowDialog() == DialogResult.OK && securityManager.IsAuth)
{
Application.Run(DependencyManager.Instance.Resolve<FormMain>());
Application.Run(new FormMain());
}
}
}