MainWindowViewModel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using AutoMapper;
  2. using BizService;
  3. using BlankApp1.Common;
  4. using BlankApp1.Views;
  5. using Microsoft.Extensions.Logging;
  6. using Model.Dto;
  7. using PLCTool.Common;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.IO;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. namespace BlankApp1.ViewModels
  18. {
  19. public class MainWindowViewModel : BindableBase, IConfigureService
  20. {
  21. private readonly ILogger _logger;
  22. private readonly IRegionManager _regionManager;
  23. private readonly IMapper _mapper;
  24. private string _title = "Prism Application";
  25. private IRegionNavigationJournal journal;
  26. public string Title
  27. {
  28. get { return _title; }
  29. set { SetProperty(ref _title, value); }
  30. }
  31. public MainWindowViewModel(IRegionManager regionManager,ILogger logger,IProductService productService, IMapper mapper)
  32. {
  33. this._regionManager = regionManager;
  34. this._logger = logger;
  35. this._mapper = mapper;
  36. ButtonNavigateCommand = new DelegateCommand<string>(ButtonNavigate);
  37. BackCommand = new DelegateCommand(GoBack);
  38. NextCommand = new DelegateCommand(GoNext);
  39. JudgePLC();
  40. logger.LogInformation("info");
  41. logger.LogError("error");
  42. //_regionManager.Regions["MeunRegion"].RequestNavigate("TreeMenuView");
  43. }
  44. private void GoNext()
  45. {
  46. if(journal.CanGoForward)
  47. journal.GoForward();
  48. }
  49. private void GoBack()
  50. {
  51. if(journal.CanGoBack)
  52. journal.GoBack();
  53. }
  54. private void ButtonNavigate(string strPar)
  55. {
  56. if (strPar == null || string.IsNullOrWhiteSpace(strPar))
  57. {
  58. return;
  59. }
  60. //导航页面
  61. _regionManager.Regions["ContentRegion"].RequestNavigate(strPar,arg=>
  62. {
  63. journal=arg.Context.NavigationService.Journal;
  64. });
  65. }
  66. /// <summary>
  67. /// 配置首页初始化参数,这个Configure是实现的接口IConfigureService中定义的方法;
  68. /// </summary>
  69. public void Configure()
  70. {
  71. //默认进入简报页面
  72. _regionManager.Regions["ContentRegion"].RequestNavigate("LogView");
  73. _regionManager.Regions["MenuRegion"].RequestNavigate("TreeMenuView");
  74. _regionManager.Regions["ContentRegion"].RequestNavigate("StatisticsView");
  75. }
  76. /// <summary>
  77. /// 连接PLC
  78. /// </summary>
  79. private void JudgePLC()
  80. {
  81. Task.Run(async () =>
  82. {
  83. while (true)
  84. {
  85. //更新界面上的值
  86. Application.Current.Dispatcher.Invoke(() =>
  87. {
  88. PLCIsConnect = PLCCom.GetInstance().IsConnectPLC();
  89. });
  90. await Task.Delay(5000);
  91. }
  92. });
  93. }
  94. #region 命令绑定
  95. public DelegateCommand<string> ButtonNavigateCommand { set; get; }
  96. public DelegateCommand NextCommand { set; get; }
  97. public DelegateCommand BackCommand { set; get; }
  98. #endregion
  99. #region 数据绑定
  100. private bool plcIsConnect = false;
  101. public bool PLCIsConnect
  102. {
  103. get { return plcIsConnect; }
  104. set { plcIsConnect = value; RaisePropertyChanged(); }
  105. }
  106. #endregion
  107. }
  108. }