终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

252 строки
8.0 KiB

  1. using BPASmartClient.Message;
  2. using Microsoft.Toolkit.Mvvm.ComponentModel;
  3. using Microsoft.Toolkit.Mvvm.Input;
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Media;
  15. using System.Windows.Threading;
  16. namespace BPASmartClient.ViewModel
  17. {
  18. /// <summary>
  19. /// 日志界面
  20. /// </summary>
  21. public class LogViewModel :ObservableObject
  22. {
  23. public DispatcherTimer dispatcherTimer;
  24. private ObservableCollection<LogModel> _LogModels;
  25. public ObservableCollection<LogModel> LogDataGrid
  26. {
  27. get
  28. {
  29. return _LogModels;
  30. }
  31. set
  32. {
  33. if (_LogModels == value)
  34. return;
  35. _LogModels = value;
  36. OnPropertyChanged("LogDataGrid");
  37. }
  38. }
  39. public LogViewModel()
  40. {
  41. LogDataGrid = new ObservableCollection<LogModel>();
  42. MessageLog.GetInstance.InfoNotify = new Action<string>((s) =>
  43. {
  44. System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
  45. {
  46. LogDataGrid.Insert(0,new LogModel { message = s,type = "Info" });
  47. }));
  48. });
  49. MessageLog.GetInstance.ExInfoNotify = new Action<string>((s) =>
  50. {
  51. System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
  52. {
  53. LogDataGrid.Insert(0,new LogModel { message = s,type = "Error" });
  54. }));
  55. });
  56. ExcelCommand = new RelayCommand(() =>
  57. {
  58. ExcellOrder();
  59. });
  60. //dispatcherTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };//1秒一流转
  61. //dispatcherTimer.Tick += delegate
  62. //{
  63. // if (TimedClear)
  64. // {
  65. // }
  66. //};
  67. //dispatcherTimer.Start();
  68. }
  69. /// <summary>
  70. /// 导出数据
  71. /// </summary>
  72. public void ExcellOrder()
  73. {
  74. if (LogDataGrid.Count > 0)
  75. {
  76. string text = "时间 类型 日志内容\n";
  77. LogDataGrid?.ToList().ForEach(temp =>
  78. {
  79. text = text + temp.time + " " + temp.type + " " + temp.message+ "\n";
  80. });
  81. SaveFileDialog openfile = new SaveFileDialog();
  82. openfile.Filter = "Txt文件(*.txt)|*.txt";
  83. if (openfile.ShowDialog() == false)
  84. {
  85. return;
  86. }
  87. string path = openfile.FileName;
  88. if (!System.IO.File.Exists(path))
  89. {
  90. //没有则创建这个文件
  91. FileStream fs1 = new FileStream(path,FileMode.Create,FileAccess.Write);//创建写入文件
  92. StreamWriter sw = new StreamWriter(fs1);
  93. sw.WriteLine(text);//开始写入值
  94. sw.Close();
  95. fs1.Close();
  96. }
  97. else
  98. {
  99. FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Write);
  100. StreamWriter sr = new StreamWriter(fs);
  101. sr.WriteLine(text);//开始写入值
  102. sr.Close();
  103. fs.Close();
  104. }
  105. string msg = string.Format("记录导出完成,共导出记录{0}条,是否打开!",LogDataGrid.Count);
  106. if (System.Windows.MessageBox.Show(msg,"提示",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
  107. {
  108. OpenFile(openfile.FileName);
  109. }
  110. }
  111. else
  112. System.Windows.MessageBox.Show("无数据!");
  113. }
  114. /// <summary>
  115. /// 打开指定路径下文件,比如:Word、Excel、Dll、图片等都可以(前提是你已经安装打开程序的对应软件)
  116. /// </summary>
  117. /// <param name="NewFileName">eg:D:\Test\模版8.doc</param>
  118. /// <param name="NewFileName">eg:D:\Test\模版8.doc</param>
  119. private void OpenFile(string NewFileName)
  120. {
  121. Process process = new Process();
  122. ProcessStartInfo processStartInfo = new ProcessStartInfo(NewFileName);
  123. process.StartInfo = processStartInfo;
  124. #region 下面这段被注释掉代码(可以用来全屏打开代码)
  125. //建立新的系统进程
  126. ////System.Diagnostics.Process process = new System.Diagnostics.Process();
  127. //设置文件名,此处为图片的真实路径 + 文件名(需要有后缀)
  128. ////process.StartInfo.FileName = NewFileName;
  129. //此为关键部分。设置进程运行参数,此时为最大化窗口显示图片。
  130. ////process.StartInfo.Arguments = "rundll32.exe C://WINDOWS//system32//shimgvw.dll,ImageView_Fullscreen";
  131. // 此项为是否使用Shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true
  132. process.StartInfo.UseShellExecute = true;
  133. #endregion
  134. try
  135. {
  136. process.Start();
  137. try
  138. {
  139. // process.WaitForExit();
  140. }
  141. catch (Exception ex)
  142. {
  143. throw ex;
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. throw ex;
  149. }
  150. finally
  151. {
  152. try
  153. {
  154. if (process != null)
  155. {
  156. process.Close();
  157. process = null;
  158. }
  159. }
  160. catch { }
  161. }
  162. }
  163. private bool _RealTimeModel = true;
  164. public bool RealTimeModel
  165. {
  166. get
  167. {
  168. return _RealTimeModel;
  169. }
  170. set
  171. {
  172. if (_RealTimeModel == value)
  173. return;
  174. _RealTimeModel = value;
  175. OnPropertyChanged("RealTimeModel");
  176. }
  177. }
  178. private bool _TimedClear = true;
  179. public bool TimedClear
  180. {
  181. get
  182. {
  183. return _TimedClear;
  184. }
  185. set
  186. {
  187. if (_TimedClear == value)
  188. return;
  189. _TimedClear = value;
  190. OnPropertyChanged("TimedClear");
  191. }
  192. }
  193. public RelayCommand ExcelCommand { get; set; }
  194. }
  195. public class LogModel :ObservableObject
  196. {
  197. public string time { get; set; }
  198. private string _type;
  199. public string type
  200. {
  201. get
  202. {
  203. return _type;
  204. }
  205. set
  206. {
  207. if (_type == value)
  208. return;
  209. _type = value;
  210. if(_type== "Error") foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#ed0032"));
  211. OnPropertyChanged("type");
  212. }
  213. }
  214. public string message { get; set; }
  215. private Brush _foreground;
  216. public Brush foreground
  217. {
  218. get
  219. {
  220. return _foreground;
  221. }
  222. set
  223. {
  224. if (_foreground == value)
  225. return;
  226. _foreground = value;
  227. OnPropertyChanged("foreground");
  228. }
  229. }
  230. public LogModel()
  231. {
  232. foreground=new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#21bb2e"));
  233. time =DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  234. }
  235. }
  236. }