using ConsoleApp; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SlnMesnac.Business.business; using SlnMesnac.Common; using SlnMesnac.Repository.service; using SlnMesnac.WPF.Page; using SlnMesnac.WPF.Page.Generate; using System; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; namespace SlnMesnac.WPF.ViewModel { public class MainWindowViewModel: INotifyPropertyChanged { private readonly ILogger _logger; //代码生成 private readonly GenerateControl generateControl = new GenerateControl(); private readonly EmployeeLoginPage _employeeLoginPage = new EmployeeLoginPage(); private readonly ExecutePage _executePage = new ExecutePage(); private IBaseStaffService _baseStaffService; private System.Timers.Timer timer; public delegate void UIDCodeDelegate(string code); public event UIDCodeDelegate UIDCodeDelegateEvent; #region 参数定义 ///// ///// PLC设备状态 ///// //private int _PlcStatus = 0; //public int PlcStatus //{ // get { return _PlcStatus; } // set { _PlcStatus = value; RaisePropertyChanged(nameof(PlcStatus)); } //} ///// ///// 打卡机状态 ///// //private int _clockStatus = 0; //public int ClockStatus //{ // get { return _clockStatus; } // set { _clockStatus = value; RaisePropertyChanged(nameof(ClockStatus)); } //} ///// ///// 扫码枪状态 ///// //private int _codeGunStatus = 0; //public int CodeGunStatus //{ // get { return _codeGunStatus; } // set { _codeGunStatus = value; RaisePropertyChanged(nameof(CodeGunStatus)); } //} public System.Windows.Controls.UserControl _content; public System.Windows.Controls.UserControl UserContent { get { return _content; } set { _content = value; OnPropertyChanged("UserContent"); } } #endregion #region 事件定义 /// /// 界面跳转按钮事件 /// public RelayCommand ControlOnClickCommand { get; set; } /// /// 打开系统键盘 /// public RelayCommand OpenSystemKeyboardCommand { get; set; } /// /// 窗体控制 /// public RelayCommand FormControlCommand { get; set; } #endregion public MainWindowViewModel() { //UserContent = _employeeLoginPage; UserContent = _executePage; _logger = App.ServiceProvider.GetService>(); var info = App.ServiceProvider.GetService(); ControlOnClickCommand = new RelayCommand(obj => ControlOnClick(obj)); OpenSystemKeyboardCommand = new RelayCommand(obj => OpenSystemKeyboard(obj)); FormControlCommand = new RelayCommand(x => FormControl(x)); timer = new System.Timers.Timer(1000); // 1 second interval timer.Elapsed += Timer_Tick; timer.Enabled = true; } /// /// 按钮文字转换事件 /// public event PropertyChangedEventHandler PropertyChanged; #region 参数 /// /// 当前时间 /// private string _currentTimeText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); public string CurrentTimeText { get { return _currentTimeText; } set { _currentTimeText = value; OnPropertyChanged("CurrentTimeText"); } } #endregion /// /// 窗体控制 /// /// private void FormControl(object obj) { try { string controlType = obj as string; switch (controlType) { // 关闭当前窗口 case "Exit": //Environment.Exit(0); Application.Current.Shutdown(); break; case "Generate": UserContent = generateControl; break; // 还原 或者 最大化当前窗口 case "Normal": if (Application.Current.MainWindow.WindowState == WindowState.Normal) { Application.Current.MainWindow.WindowState = WindowState.Maximized; break; } if (Application.Current.MainWindow.WindowState == WindowState.Maximized) { Application.Current.MainWindow.WindowState = WindowState.Normal; break; } break; // 最小化当前窗口 case "Minimized": Application.Current.MainWindow.WindowState = WindowState.Minimized; break; default: break; } } catch (Exception ex) { _logger.LogError("窗体控制逻辑异常", ex); } } /// /// 打开系统键盘 /// /// private void OpenSystemKeyboard(object obj) { System.Diagnostics.Process.Start("osk.exe"); } /// /// 界面跳转 /// private void ControlOnClick(object obj) { try { string info = obj as string; //UserContent = inStoreInfoControl; switch (info) { case "Execute": UserContent = _executePage; break; case "Employee": UserContent = _employeeLoginPage; break; default: UserContent = _executePage; break; } } catch (Exception ex) { _logger.LogError("界面跳转逻辑异常", ex); } } private void Timer_Tick(object sender, ElapsedEventArgs e) { CurrentTimeText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } public void OnPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }