Commit 049d40ac authored by Rabbit's avatar Rabbit
Browse files

XML文件读取

parent b58ab1bb
Loading
Loading
Loading
Loading
+185 −8
Original line number Diff line number Diff line
@@ -162,6 +162,92 @@ namespace CountDownList.Class
            }
        }

        public async void AddCountDownLabelRaw(ListLabel label, int? InsertIndex = null)
        {
            //自动释放资源
            using (ListLabel lb = label)
            {
                bool Move = false;

                //事件绑定
                lb.OnEdit += CountDown_OnEdit;
                lb.OnDelete += CountDown_OnDelete;
                lb.OnShare += CountDown_OnShare;

                if (InsertIndex != null)
                {
                    try
                    {
                        DoingPanel.Children.Insert((int)InsertIndex, lb);
                    }
                    catch (System.ArgumentOutOfRangeException)
                    {
                        DoingPanel.Children.Add(lb);
                    }
                }
                else
                {
                    DoingPanel.Children.Add(lb);
                }

                while (!lb.IsDelete)
                {
                    if (DateTime.Now <= lb.EndTime)
                    {
                        //当不是正计时时执行
                        //当展开框展开时才刷新控件,以优化性能

                        if (DoingE.IsExpanded && IsOpen)
                        {
                            if (IsUserVisible(lb, RootWindow))
                            {
                                lb.SyncProgress();
                                lb.SyncTime();
                                lb.SyncTitle();
                            }
                        }
                    }
                    else
                    {
                        //当不是正计时并且已完成
                        //完成时移入“已完成”
                        if (!Move)
                        {
                            Move = true;//运行一次

                            DoingPanel.Children.Remove(lb);//从进行中移除控件
                            WcPanel.Children.Add(lb);//添加到已完成

                            if (!IsUserVisible(lb, RootWindow))
                            {
                                await Task.Delay(250);
                            }
                        }

                        if (WcE.IsExpanded && IsOpen && IsUserVisible(lb, RootWindow))
                        {
                            if (IsUserVisible(lb, RootWindow))
                            {
                                //当展开框展开时才刷新控件,以优化性能
                                lb.SyncProgress();
                                lb.SyncTime();
                                lb.SyncTitle();
                            }
                        }
                    }

                    if (lb.IsMouseEnter || lb.Open)
                    {
                        await Task.Delay(15);
                    }
                    else
                    {
                        await Task.Delay(75);
                    }
                }
            }
        }

        /// <summary>
        /// 标签分享事件
        /// </summary>
@@ -181,9 +267,16 @@ namespace CountDownList.Class
        private void CountDown_OnDelete(object? sender, EventArgs e)
        {
            if (sender != null)
            {
                if (((Controls.ListLabel)sender).IsReadOnly == false)
                {
                    DeleteForElement((Controls.ListLabel)sender);
                }
                else
                {
                    HandyControl.Controls.MessageBox.Show("删除失败,此标签禁止删除", $"删除 {((Controls.ListLabel)sender).Title}", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        /// <summary>
@@ -194,9 +287,16 @@ namespace CountDownList.Class
        private void CountDown_OnEdit(object? sender, EventArgs e)
        {
            if (sender != null)
            {
                if (((Controls.ListLabel)sender).IsReadOnly == false)
                {
                    EditAndChange((Controls.ListLabel)sender);
                }
                else
                {
                    HandyControl.Controls.MessageBox.Show("删除失败,此标签禁止删除", $"删除 {((Controls.ListLabel)sender).Title}", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        /// <summary>
@@ -262,6 +362,58 @@ namespace CountDownList.Class
            }
        }

        public async void AddCountLabelRaw(ListLabel label, int? InsertIndex = null)
        {
            using (ListLabel lb = label)
            {
                //事件绑定
                lb.OnEdit += CountDown_OnEdit;
                lb.OnDelete += CountDown_OnDelete;
                lb.OnShare += CountDown_OnShare;

                if (InsertIndex != null)
                {
                    try
                    {
                        CountPanel.Children.Insert((int)InsertIndex, lb);
                    }
                    catch (System.ArgumentOutOfRangeException)
                    {
                        CountPanel.Children.Add(lb);
                    }
                }
                else
                {
                    CountPanel.Children.Add(lb);
                }


                while (true)
                {
                    //当不是正计时时执行
                    //当展开框展开时才刷新控件,以优化性能
                    if (CountE.IsExpanded && IsOpen)
                    {
                        if (IsUserVisible(lb, RootWindow))
                        {
                            lb.SyncProgress();
                            lb.SyncTime();
                            lb.SyncTitle();
                        }
                    }

                    if (lb.IsMouseEnter || lb.Open)
                    {
                        await Task.Delay(15);
                    }
                    else
                    {
                        await Task.Delay(75);
                    }
                }
            }
        }

        /// <summary>
        /// 通过对比标题从容器中删除标签
        /// </summary>
@@ -465,6 +617,11 @@ namespace CountDownList.Class

        }

        /// <summary>
        /// 编辑标签事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Home_OnDelete(object? sender, EventArgs e)
        {
            ListLabel? delElememt;
@@ -473,18 +630,31 @@ namespace CountDownList.Class
                if ((ListP)((Home)sender).listView.SelectedItem != null)
                {
                    delElememt = ((ListP)((Home)sender).listView.SelectedItem).Own;

                    if (delElememt.IsReadOnly == false)
                    {
                        DeleteForElement(delElememt);
                        if (sender != null)
                        {
                            ((Home)sender).RefreshList(ScanList());
                        }
                    }
                    else
                    {
                        HandyControl.Controls.MessageBox.Show("删除失败,此标签禁止删除", $"删除 {delElememt.Title}", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// 删除标签事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Home_OnEdit(object? sender, EventArgs e)
        {
            ListLabel? editElement;
@@ -493,12 +663,19 @@ namespace CountDownList.Class
                if ((ListP)((Home)sender).listView.SelectedItem != null)
                {
                    editElement = ((ListP)((Home)sender).listView.SelectedItem).Own;
                    if (editElement.IsReadOnly == false)
                    {
                        EditAndChange(editElement);
                        if (sender != null)
                        {
                            ((Home)sender).RefreshList(ScanList());
                        }
                    }
                    else
                    {
                        HandyControl.Controls.MessageBox.Show("编辑失败,此标签禁止删除", $"删除 {editElement.Title}", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
            catch
            {
+144 −32
Original line number Diff line number Diff line
using System;
using System.Collections.Generic;
using DW = System.Drawing;
using System.Linq;
using System.Reflection.Metadata;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml;
using System.Xml.Linq;
using static OpenCvSharp.ML.DTrees;

#pragma warning disable
namespace CountDownList.Class
{
    public class CountCownListXml
    {
        public required XDocument xmlDoc;
        public required XElement root;
        public required XElement main;
        public XmlDocument xmlDoc = new();
        public XElement root;
        public XElement main;
        public CountCownListXml() 
        {
            xmlDoc = XDocument.Load($"{AppDomain.CurrentDomain.BaseDirectory}\\Data\\CountDownList.xml");
            xmlDoc.Load($"{AppDomain.CurrentDomain.BaseDirectory}\\Data\\CountDownList.xml");
        }

            try
        public void ReadListLabels(List<Controls.ListLabel> labels)
        {
                if (xmlDoc.Root != null && root != null)
            XmlNode rootNode = xmlDoc.SelectSingleNode("shRabbit").SelectSingleNode("CountDownList");
            foreach (XmlNode item in rootNode.ChildNodes)
            {
                    root = xmlDoc.Root;
                    main = root.Element("CountDownList");
                item.RemoveAll();
            }
                else

            //添加一个带有属性的节点信息
            foreach (Controls.ListLabel label in labels)
            {
                    throw (new("找不到Xml配置文件节点"));
                XmlElement newElement = xmlDoc.CreateElement("ListLabel");
                
                //newElement.att

                rootNode.AppendChild(newElement);
            }
            //保存更改
            xmlDoc.Save($"{AppDomain.CurrentDomain.BaseDirectory}\\Data\\CountDownList.xml");
        }
            catch

        public List<Controls.ListLabel> GetListLabels()
        {
            List<Controls.ListLabel> result = new();

            XmlNode rootNode = xmlDoc.SelectSingleNode("shRabbit").SelectSingleNode("CountDownList");

            foreach (XmlNode item in rootNode.ChildNodes)
            {
                string title = "";
                DateTime startTime = DateTime.Now, endTime = DateTime.Now;
                bool isDarkText = false, isReadOnly = false, isCount = false;
                Brush brush = new SolidColorBrush();

                string backgroundRaw = "";

                bool isImageBackground = false;

                //获得该节点的属性集合
                XmlAttributeCollection attributeCol = item.Attributes;

                foreach (XmlAttribute attri in attributeCol)
                {
                throw (new("找不到Xml配置文件节点"));
                    switch (attri.Name)
                    {
                        case "Title":
                            title = attri.Value;
                            break;

                        case "StartTime":
                            startTime = new(Convert.ToInt64(attri.Value));
                            break;

                        case "EndTime":
                            endTime = new(Convert.ToInt64(attri.Value));
                            break;

                        case "IsCount":
                            isCount = Convert.ToBoolean(attri.Value);
                            break;

                        case "IsReadOnly":
                            isReadOnly = Convert.ToBoolean(attri.Value);
                            break;

                        case "BackgroundType":
                            switch (attri.Value)
                            {
                                case "Image":
                                    brush = new ImageBrush();
                                    isImageBackground = true;
                                    break;

                                case "Color":
                                    brush = new SolidColorBrush();
                                    isImageBackground = false;
                                    break;
                            }
                            break;

                        case "Background":
                            backgroundRaw = attri.Value;
                            break;
                    }
                }

        //public List<UIElement> GetAllListToControls()
        //{
        //    List<UIElement> e = new();
        //    foreach (XElement item in main.Elements())
        //    {
        //        Controls.ListLabel ll = new
        //        (
        //            item.Element("Title").Value,
        //            DateTime.FromBinary(Convert.ToInt64(item.Element("StartTimeT").Value)),
        //            DateTime.FromBinary(Convert.ToInt64(item.Element("EndTimeT").Value)),
        //            item.Element("Format").Value,
        //            Convert.ToBoolean(item.Element("IsCount").Value)
        //        );
        //        e.Add(ll);
        //    }
        //    return e;
        //}
                if (isImageBackground)
                {
                    ImageBrush ib = new();
                    ib.ImageSource = new BitmapImage(new Uri(backgroundRaw, UriKind.Relative));
                    ib.Stretch = Stretch.UniformToFill;
                    brush = ib;
                }
                else
                {
                    byte[] color = new byte[3];
                    string[] colorRaw = new string[3];
                    colorRaw = backgroundRaw.Split(',');
                    for (int i = 0; i < 3; i++)
                    {
                        color[i] = Convert.ToByte(colorRaw[i]);
                    }

                    SolidColorBrush sb = new(Color.FromArgb(color[0], color[1], color[2], color[3]));
                    brush = sb;
                }

                Controls.ListLabel lb = new Controls.ListLabel(title, startTime, endTime, "dd\\天hh\\:mm\\:ss", isDarkText, brush, isCount);
                lb.IsReadOnly = isReadOnly;
                result.Add(lb);
            }
            return result;
        }
    }

    /// <summary>
@@ -63,10 +146,39 @@ namespace CountDownList.Class
    /// </summary>
    public class CountDownListXmlTemplete
    {
        /// <summary>
        /// 标题
        /// </summary>
        public required string Title {  get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public required long StartTimeT { get; set; }
        public required long EndTimeT { get; set; }

        /// <summary>
        /// 结束时间,IsCount为True时可以为Null
        /// </summary>
        public required long? EndTimeT { get; set; }

        /// <summary>
        /// 是否为正计时模式
        /// </summary>
        public required bool IsCount { get; set; }

        /// <summary>
        /// 时间转换格式
        /// </summary>
        public required string format { get; set; }

        /// <summary>
        /// 背景画刷
        /// </summary>
        public required Brush Background { get; set; }

        /// <summary>
        /// 只读模式,为True的标签无法修改和删除
        /// </summary>
        public required Brush Readonly { get; set; }
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ namespace CountDownList.Controls
        public bool IsCountMode { get; set; }
        public Brush LabelBackground { get; set; }
        public bool DarkText { get; set; }
        public bool IsReadOnly { get; set; }

        public event EventHandler<EventArgs>? OnEdit;
        public event EventHandler<EventArgs>? OnDelete;
+4 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
  </PropertyGroup>

  <ItemGroup>
    <None Remove="Data\CountDownList.xml" />
    <None Remove="Data\Image\1.jpg" />
    <None Remove="Data\Image\2.jpg" />
    <None Remove="Data\Image\3.png" />
@@ -30,6 +31,9 @@
  </ItemGroup>

  <ItemGroup>
    <Content Include="Data\CountDownList.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="Data\Image\1.jpg">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
+1 −13
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8" ?>
<shRabbit>
	<CountDownList>
		<List>
			<Title>01</Title>
			<StartTimeT>0</StartTimeT>
			<EndTimeT>0</EndTimeT>
			<IsCount>False</IsCount>
			<TimeFormat>dd\天hh\:mm\:ss</TimeFormat>
			<BackGround>
				<Color>
					#FFFFFFFF
				</Color>
			</BackGround>
			<DarkText>True</DarkText>
		</List>
		<ListLabel Title="Test" IsCount="False" StartTime="638700660638405013" EndTime="638700663281575412" DarkText="False" BackgroundType="Image" Background="UI\test.png" IsReadOnly="True"/>
	</CountDownList>
</shRabbit>
Loading