From a49dc1ab39ac17474366d6c72ff10c0dc4b567a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=84=8F=20=E5=BD=AD?= <2417589739@qq.com> Date: Tue, 30 Aug 2022 10:36:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A5=E8=A1=A8=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.Business/Plugin/ConfigMgr.cs | 3 +- .../Pages/Model/DataGridColumnAttribute.cs | 43 +++++++++ ...oNotAutoGenerateDataGridColumnAttribute.cs | 12 +++ .../Pages/Model/ReportBase.cs | 15 +++ .../Pages/Model/ReportHelper.cs | 53 +++++++++++ .../Pages/Model/ReportTestModel.cs | 79 ++++++++++++++++ .../Pages/View/ReportView.xaml | 47 ++++++++++ .../Pages/View/ReportView.xaml.cs | 28 ++++++ .../Pages/ViewModel/ReportViewModel.cs | 63 +++++++++++++ .../UserControls/DataGridEx.cs | 76 +++++++++++++++ BPASmartClient/Control/LogView.xaml | 92 +++++++++++++------ DosingSystem/App.xaml.cs | 8 ++ WPFDemo/App.xaml | 2 +- WPFDemo/Bb/Class1.cs | 36 ++++++++ WPFDemo/Bb/DataGridColumnOrderAttribute.cs | 43 +++++++++ WPFDemo/Bb/DataGridEx.cs | 75 +++++++++++++++ ...oNotAutoGenerateDataGridColumnAttribute.cs | 12 +++ WPFDemo/Bb/ViewModel.cs | 30 ++++++ WPFDemo/Bb/Window1.xaml | 29 ++++++ WPFDemo/Bb/Window1.xaml.cs | 27 ++++++ WPFDemo/WPFDemo.csproj | 5 + 21 files changed, 747 insertions(+), 31 deletions(-) create mode 100644 BPASmartClient.CustomResource/Pages/Model/DataGridColumnAttribute.cs create mode 100644 BPASmartClient.CustomResource/Pages/Model/DoNotAutoGenerateDataGridColumnAttribute.cs create mode 100644 BPASmartClient.CustomResource/Pages/Model/ReportBase.cs create mode 100644 BPASmartClient.CustomResource/Pages/Model/ReportHelper.cs create mode 100644 BPASmartClient.CustomResource/Pages/Model/ReportTestModel.cs create mode 100644 BPASmartClient.CustomResource/Pages/View/ReportView.xaml create mode 100644 BPASmartClient.CustomResource/Pages/View/ReportView.xaml.cs create mode 100644 BPASmartClient.CustomResource/Pages/ViewModel/ReportViewModel.cs create mode 100644 BPASmartClient.CustomResource/UserControls/DataGridEx.cs create mode 100644 WPFDemo/Bb/Class1.cs create mode 100644 WPFDemo/Bb/DataGridColumnOrderAttribute.cs create mode 100644 WPFDemo/Bb/DataGridEx.cs create mode 100644 WPFDemo/Bb/DoNotAutoGenerateDataGridColumnAttribute.cs create mode 100644 WPFDemo/Bb/ViewModel.cs create mode 100644 WPFDemo/Bb/Window1.xaml create mode 100644 WPFDemo/Bb/Window1.xaml.cs diff --git a/BPASmartClient.Business/Plugin/ConfigMgr.cs b/BPASmartClient.Business/Plugin/ConfigMgr.cs index 82bde613..b24aaf91 100644 --- a/BPASmartClient.Business/Plugin/ConfigMgr.cs +++ b/BPASmartClient.Business/Plugin/ConfigMgr.cs @@ -51,6 +51,7 @@ namespace BPASmartClient.Business string JsonString = File.ReadAllText(path); deviceConfigModelJsons = JsonConvert.DeserializeObject>(JsonString); } + else deviceConfigModelJsons = new ObservableCollection(); if (deviceConfigModelJsons?.Count > 0) { ClientId = int.Parse(deviceConfigModelJsons.ElementAt(0).ShopId); @@ -84,7 +85,7 @@ namespace BPASmartClient.Business IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()); configurationBuilder.AddApolloConfiguration(p => { - + p.AppId = apoid; p.MetaServer = apolloUri; p.Namespaces = new List() { "application", namespa }; diff --git a/BPASmartClient.CustomResource/Pages/Model/DataGridColumnAttribute.cs b/BPASmartClient.CustomResource/Pages/Model/DataGridColumnAttribute.cs new file mode 100644 index 00000000..e685406d --- /dev/null +++ b/BPASmartClient.CustomResource/Pages/Model/DataGridColumnAttribute.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.CustomResource.Pages.Model +{ + [AttributeUsage(AttributeTargets.Property)] + public class DataGridColumnAttribute : Attribute + { + public static readonly DataGridColumnAttribute Default = new DataGridColumnAttribute(0); + + public DataGridColumnAttribute(int index) + { + DataGridColumnValue = index; + } + + public virtual int DataGridColumn => DataGridColumnValue; + + protected int DataGridColumnValue { get; set; } + + public override bool Equals(object obj) + { + if (obj == this) + { + return true; + } + + return obj is DataGridColumnAttribute other && other.DataGridColumn == DataGridColumn; + } + + public override int GetHashCode() + { + return DataGridColumn.GetHashCode(); + } + + public override bool IsDefaultAttribute() + { + return Equals(Default); + } + } +} diff --git a/BPASmartClient.CustomResource/Pages/Model/DoNotAutoGenerateDataGridColumnAttribute.cs b/BPASmartClient.CustomResource/Pages/Model/DoNotAutoGenerateDataGridColumnAttribute.cs new file mode 100644 index 00000000..6b571db5 --- /dev/null +++ b/BPASmartClient.CustomResource/Pages/Model/DoNotAutoGenerateDataGridColumnAttribute.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.CustomResource.Pages.Model +{ + public class DoNotAutoGenerateDataGridColumnAttribute : Attribute + { + } +} diff --git a/BPASmartClient.CustomResource/Pages/Model/ReportBase.cs b/BPASmartClient.CustomResource/Pages/Model/ReportBase.cs new file mode 100644 index 00000000..baeb3623 --- /dev/null +++ b/BPASmartClient.CustomResource/Pages/Model/ReportBase.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel; +using Microsoft.Toolkit.Mvvm.ComponentModel; + +namespace BPASmartClient.CustomResource.Pages.Model +{ + [Serializable] + public class ReportBase : ObservableObject + { + } +} diff --git a/BPASmartClient.CustomResource/Pages/Model/ReportHelper.cs b/BPASmartClient.CustomResource/Pages/Model/ReportHelper.cs new file mode 100644 index 00000000..701f2e9c --- /dev/null +++ b/BPASmartClient.CustomResource/Pages/Model/ReportHelper.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Collections.ObjectModel; +using BPASmartClient.Helper; + + +namespace BPASmartClient.CustomResource.Pages.Model +{ + public class ReportHelper + { + public ObservableCollection Reports { get; set; } = new ObservableCollection(); + + public void Add(ReportBase report) + { + Reports.Add(report); + } + + public void Save(string name) + { + DataRecord record = new DataRecord(); + record.Save(Reports, name); + } + + public void Dispos() + { + Reports.Clear(); + } + + public ObservableCollection GetData(string path) + { + DataRecord record = new DataRecord(); + return record.Read>(path); + } + } + + //public class ReportHelper + //{ + + // private volatile static ReportHelper _Instance; + // public static ReportHelper GetInstance => _Instance ?? (_Instance = new ReportHelper()); + // private ReportHelper() { } + + // public object GetData(string path) + // { + // DataRecord record = new DataRecord(); + // return record.Read(path); + // } + + //} +} diff --git a/BPASmartClient.CustomResource/Pages/Model/ReportTestModel.cs b/BPASmartClient.CustomResource/Pages/Model/ReportTestModel.cs new file mode 100644 index 00000000..3a0e1751 --- /dev/null +++ b/BPASmartClient.CustomResource/Pages/Model/ReportTestModel.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Toolkit.Mvvm.ComponentModel; + +namespace BPASmartClient.CustomResource.Pages.Model +{ + [Serializable] + public class ReportTestModel : ReportBase + { + //[DataGridColumn(0)]//设置列的索引 + //[DisplayName("整数")]//设置设备名称 + //public int data1 { get { return _mdata1; } set { _mdata1 = value; OnPropertyChanged(); } } + //private int _mdata1; + + + //[DataGridColumn(1)] + //[DisplayName("字节")] + //public byte data2 { get { return _mdata2; } set { _mdata2 = value; OnPropertyChanged(); } } + //private byte _mdata2; + + + //[DataGridColumn(2)] + //[DisplayName("布尔")] + //public bool data3 { get { return _mdata3; } set { _mdata3 = value; OnPropertyChanged(); } } + //private bool _mdata3; + + + //[DataGridColumn(3)] + //[DisplayName("字符串")] + //public string data4 { get { return _mdata4; } set { _mdata4 = value; OnPropertyChanged(); } } + //private string _mdata4; + + + //[DataGridColumn(4)] + //[DisplayName("浮点数")] + //public float data5 { get { return _mdata5; } set { _mdata5 = value; OnPropertyChanged(); } } + //private float _mdata5; + + + //[DataGridColumn(5)] + //[DisplayName("无符号整数")] + //public ushort data6 { get { return _mdata6; } set { _mdata6 = value; OnPropertyChanged(); } } + //private ushort _mdata6; + + [DataGridColumn(0)]//设置列的索引 + [DisplayName("整数")]//设置设备名称 + public int data1 { get; set; } + + + [DataGridColumn(1)] + [DisplayName("字节")] + public byte data2 { get; set; } + + + [DataGridColumn(2)] + [DisplayName("布尔")] + public bool data3 { get; set; } + + + [DataGridColumn(3)] + [DisplayName("字符串")] + public string data4 { get; set; } + + + [DataGridColumn(4)] + [DisplayName("浮点数")] + public float data5 { get; set; } + + + [DataGridColumn(5)] + [DisplayName("无符号整数")] + public ushort data6 { get; set; } + + } +} diff --git a/BPASmartClient.CustomResource/Pages/View/ReportView.xaml b/BPASmartClient.CustomResource/Pages/View/ReportView.xaml new file mode 100644 index 00000000..05334683 --- /dev/null +++ b/BPASmartClient.CustomResource/Pages/View/ReportView.xaml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + - + + - + - + - + - + @@ -57,17 +83,25 @@ --> - + - + - + - + diff --git a/DosingSystem/App.xaml.cs b/DosingSystem/App.xaml.cs index 7603d88f..dee9ed09 100644 --- a/DosingSystem/App.xaml.cs +++ b/DosingSystem/App.xaml.cs @@ -156,6 +156,14 @@ namespace BPASmartClient.DosingSystem ToggleWindowPath = "Pages.View.DebugLogView" }); + InfoLog.Add(new SubMenumodel() + { + SubMenuName = "报表数据", + SubMenuPermission = new Permission[] { Permission.操作员, Permission.管理员, Permission.技术员, Permission.技术员 }, + AssemblyName = "BPASmartClient.CustomResource", + ToggleWindowPath = "Pages.View.ReportView" + }); + MenuManage.GetInstance.menuModels.Add(new MenuModel() { MainMenuIcon = "", diff --git a/WPFDemo/App.xaml b/WPFDemo/App.xaml index 7cdf221a..98839182 100644 --- a/WPFDemo/App.xaml +++ b/WPFDemo/App.xaml @@ -3,6 +3,6 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WPFDemo" - StartupUri="MainWindow.xaml"> + StartupUri="Bb/Window1.xaml"> diff --git a/WPFDemo/Bb/Class1.cs b/WPFDemo/Bb/Class1.cs new file mode 100644 index 00000000..ed41be82 --- /dev/null +++ b/WPFDemo/Bb/Class1.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WPFDemo.Bb +{ + public class Class1 + { + [DataGridColumnOrder(0)]//设置列的索引 + [DisplayName("整数")]//设置设备名称 + public int data1 { get; set; } + + [DataGridColumnOrder(1)] + [DisplayName("字节")] + public byte data2 { get; set; } + + [DataGridColumnOrder(2)] + [DisplayName("布尔")] + public bool data3 { get; set; } + + [DataGridColumnOrder(3)] + [DisplayName("字符串")] + public string data4 { get; set; } + + [DataGridColumnOrder(4)] + [DisplayName("浮点数")] + public float data5 { get; set; } + + [DataGridColumnOrder(5)] + [DisplayName("无符号整数")] + public ushort data6 { get; set; } + } +} diff --git a/WPFDemo/Bb/DataGridColumnOrderAttribute.cs b/WPFDemo/Bb/DataGridColumnOrderAttribute.cs new file mode 100644 index 00000000..0cabad8f --- /dev/null +++ b/WPFDemo/Bb/DataGridColumnOrderAttribute.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WPFDemo.Bb +{ + [AttributeUsage(AttributeTargets.Property)] + public class DataGridColumnOrderAttribute : Attribute + { + public static readonly DataGridColumnOrderAttribute Default = new DataGridColumnOrderAttribute(0); + + public DataGridColumnOrderAttribute(int order) + { + DataGridColumnOrderValue = order; + } + + public virtual int DataGridColumnOrder => DataGridColumnOrderValue; + + protected int DataGridColumnOrderValue { get; set; } + + public override bool Equals(object obj) + { + if (obj == this) + { + return true; + } + + return obj is DataGridColumnOrderAttribute other && other.DataGridColumnOrder == DataGridColumnOrder; + } + + public override int GetHashCode() + { + return DataGridColumnOrder.GetHashCode(); + } + + public override bool IsDefaultAttribute() + { + return Equals(Default); + } + } +} diff --git a/WPFDemo/Bb/DataGridEx.cs b/WPFDemo/Bb/DataGridEx.cs new file mode 100644 index 00000000..1876ed38 --- /dev/null +++ b/WPFDemo/Bb/DataGridEx.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Controls; + +namespace WPFDemo.Bb +{ + public class DataGridEx : DataGrid + { + private List _columnAndOrderList = new List(); + + public DataGridEx() + { + AutoGeneratingColumn += DataGridEx_AutoGeneratingColumn; + AutoGeneratedColumns += DataGridEx_AutoGeneratedColumns; + } + + private void DataGridEx_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) + { + + e.Cancel = true;//将创建过程标记为已取消,以阻止DataGrid将生成的列对象加入到列集合中. + + //从Attribute中读取显示名称. + var displayName = ((System.ComponentModel.MemberDescriptor)e.PropertyDescriptor).DisplayName; + if (!string.IsNullOrWhiteSpace(displayName)) + { + e.Column.Header = displayName; + } + + //从Attribute中读取顺序,以及是否自动生成列的标识. + var attributes = ((System.ComponentModel.MemberDescriptor)e.PropertyDescriptor).Attributes; + var order = 0; + foreach (Attribute attribute in attributes) + { + if (attribute is DataGridColumnOrderAttribute orderAttribute) + { + order = orderAttribute.DataGridColumnOrder; + } + else if (attribute is DoNotAutoGenerateDataGridColumnAttribute) + { + //发现 DoNotAutoGenerateDataGridColumnAttribute 时,丢弃已生成的列 + return; + } + } + + //将创建的列及顺序保存 + _columnAndOrderList.Add(new ColumnAndOrder(order, e.Column)); + } + + private void DataGridEx_AutoGeneratedColumns(object sender, EventArgs e) + { + //按顺序将所有列加入到Columns集合中 + foreach (var columnAndOrder in _columnAndOrderList.OrderBy(v => v.Order)) + { + Columns.Add(columnAndOrder.DataGridColumn); + } + _columnAndOrderList = null; + } + + private class ColumnAndOrder + { + public ColumnAndOrder(int order, DataGridColumn dataGridColumn) + { + Order = order; + DataGridColumn = dataGridColumn; + } + + public int Order { get; } + + public DataGridColumn DataGridColumn { get; } + } + } +} diff --git a/WPFDemo/Bb/DoNotAutoGenerateDataGridColumnAttribute.cs b/WPFDemo/Bb/DoNotAutoGenerateDataGridColumnAttribute.cs new file mode 100644 index 00000000..c31e5d3a --- /dev/null +++ b/WPFDemo/Bb/DoNotAutoGenerateDataGridColumnAttribute.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WPFDemo.Bb +{ + public class DoNotAutoGenerateDataGridColumnAttribute : Attribute + { + } +} diff --git a/WPFDemo/Bb/ViewModel.cs b/WPFDemo/Bb/ViewModel.cs new file mode 100644 index 00000000..bf6ba8c3 --- /dev/null +++ b/WPFDemo/Bb/ViewModel.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Toolkit.Mvvm.ComponentModel; +using System.Collections.ObjectModel; +namespace WPFDemo.Bb +{ + public class ViewModel : ObservableObject + { + public ViewModel() + { + for (int i = 0; i < 30; i++) + { + class1s.Add(new Class1() + { + data1 = new Random().Next(1000, 2000), + data2 = (byte)new Random().Next(0, 240), + data3 = Convert.ToBoolean(new Random().Next(0, 1)), + data4 = new Random().Next(3000, 4000).ToString(), + data5 = (float)new Random().NextDouble() * 100, + data6 = (ushort)new Random().Next(5000, 6000) + }); + } + } + + public ObservableCollection class1s { get; set; } = new ObservableCollection(); + } +} diff --git a/WPFDemo/Bb/Window1.xaml b/WPFDemo/Bb/Window1.xaml new file mode 100644 index 00000000..100b26c1 --- /dev/null +++ b/WPFDemo/Bb/Window1.xaml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + diff --git a/WPFDemo/Bb/Window1.xaml.cs b/WPFDemo/Bb/Window1.xaml.cs new file mode 100644 index 00000000..002d874f --- /dev/null +++ b/WPFDemo/Bb/Window1.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; + +namespace WPFDemo.Bb +{ + /// + /// Window1.xaml 的交互逻辑 + /// + public partial class Window1 : Window + { + public Window1() + { + InitializeComponent(); + } + } +} diff --git a/WPFDemo/WPFDemo.csproj b/WPFDemo/WPFDemo.csproj index e6eb518d..9f859991 100644 --- a/WPFDemo/WPFDemo.csproj +++ b/WPFDemo/WPFDemo.csproj @@ -12,12 +12,17 @@ + + + + + Always