using GalaSoft.MvvmLight; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Win32; using SlnMesnac.Business; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service; using SqlSugar; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Media; namespace SlnMesnac.WPF.ViewModel { public partial class IndexViewModel: ObservableObject { private ILogoIdentifyService logoIdentifyService; public IndexViewModel() { logoIdentifyService = App.ServiceProvider.GetService(); LogoBusiness.RefreshMessageEvent += RefreshMessage; LogoBusiness.RefreshBoxInfoEvent += RefreshBoxInfo; RefreshDataGrid(); } /// /// 测试方法 /// /// public byte[] OpenAndConvertImageToBytes() { byte[] imageData = null; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png)|*.jpg;*.jpeg;*.png"; if (openFileDialog.ShowDialog() == true) { string filePath = openFileDialog.FileName; try { using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { imageData = new byte[stream.Length]; stream.Read(imageData, 0, (int)stream.Length); } } catch (Exception ex) { // 处理异常 Console.WriteLine("Error: " + ex.Message); } } return imageData; } public async Task RefreshDataGrid() { await Task.Run(() => { List list = logoIdentifyService.GetAllRecord(); if (list == null || list.Count == 0) return; App.Current.Dispatcher.BeginInvoke((Action)(() => { LogoIdentifyDataGrid.Clear(); list = list.OrderByDescending(x=>x.RecordTime).ToList(); foreach (LogoIdentify verify in list) { LogoIdentifyDataGrid.Add(verify); } })); }); } #region 界面刷新 /// /// 刷新界面箱体信息 /// /// /// /// /// public void RefreshBoxInfo(string boxCode,string boxTime,string model,byte[] imageData,bool isSuccess) { BoxCode = boxCode; BoxTime = boxTime; ProductModel = model; ImageData = imageData; RefreshResultColor(isSuccess); RefreshDataGrid(); } /// /// 刷新界面提示信息 /// /// public void RefreshMessage(string message,bool isWarning = false) { if (isWarning) { MsgColor = System.Windows.Media.Brushes.Red; } else { MsgColor = System.Windows.Media.Brushes.White; } Message = $"{DateTime.Now} :{message}"; } #endregion #region 界面参数定义 #region 初始化datagrid private ObservableCollection _LogoIdentifyDataGrid = new ObservableCollection(); public ObservableCollection LogoIdentifyDataGrid { get { return _LogoIdentifyDataGrid; } set { _LogoIdentifyDataGrid = value; RaisePropertyChanged();//属性通知 } } #endregion #region 箱体码 private string _BoxCode; public string BoxCode { get { return _BoxCode; } set { _BoxCode = value; RaisePropertyChanged(nameof(BoxCode)); } } #endregion #region 箱体码扫描时间 private string _BoxTime; public string BoxTime { get { return _BoxTime; } set { _BoxTime = value; RaisePropertyChanged(nameof(BoxTime)); } } #endregion #region 产品型号 private string _ProductModel; public string ProductModel { get { return _ProductModel; } set { _ProductModel = value; RaisePropertyChanged(nameof(ProductModel)); } } #endregion #region 提示信息 private string _Message; public string Message { get { return _Message; } set { _Message = value; RaisePropertyChanged(nameof(Message)); } } private System.Windows.Media.Brush _msgColor; public System.Windows.Media.Brush MsgColor { get { return _msgColor; } set { _msgColor = value; RaisePropertyChanged(nameof(MsgColor)); } } #endregion #region Logo照片 private byte[] _imageData; public byte[] ImageData { get => _imageData; set { _imageData = value; RaisePropertyChanged(nameof(ImageData)); // 属性改变时触发通知 } } #endregion #region Logo识别结果 private string _resultTxt; public string ResultTxt { get { return _resultTxt; } set { _resultTxt = value; RaisePropertyChanged(nameof(ResultTxt)); } } private System.Windows.Media.Brush _resultColor; public System.Windows.Media.Brush ResultColor { get { return _resultColor; } set { _resultColor = value; RaisePropertyChanged(nameof(ResultColor)); } } public void RefreshResultColor(bool isSuccess) { if (isSuccess) { ResultTxt = "成功"; ResultColor = System.Windows.Media.Brushes.LimeGreen; } else { ResultTxt = "失败"; ResultColor = System.Windows.Media.Brushes.Red; } } #endregion #endregion } }