RepeatMarkDialogViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using PDF_Master.CustomControl;
  2. using PDF_Master.Helper;
  3. using PDF_Master.Model;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace PDF_Master.ViewModels.Dialog.Redaction
  13. {
  14. public class RepeatMarkDialogViewModel : BindableBase, IDialogAware
  15. {
  16. public string Title => "";
  17. public event Action<IDialogResult> RequestClose;
  18. public DelegateCommand OkCommand { get; set; }
  19. public DelegateCommand CancelCommand { get; set; }
  20. public DelegateCommand<string> CheckedCommand { get; set; }
  21. private int pageCount;
  22. /// <summary>
  23. /// 最大页码
  24. /// </summary>
  25. public int PageCount
  26. {
  27. get { return pageCount; }
  28. set
  29. {
  30. SetProperty(ref pageCount, value);
  31. }
  32. }
  33. private string inputString;
  34. public string InputString
  35. {
  36. get { return inputString; }
  37. set
  38. {
  39. SetProperty(ref inputString, value);
  40. if (!string.IsNullOrEmpty(value))
  41. {
  42. CheckCustomPage();
  43. }
  44. }
  45. }
  46. public List<int> PageList = new List<int>();
  47. public RepeatMarkDialogViewModel()
  48. {
  49. OkCommand = new DelegateCommand(ok);
  50. CancelCommand = new DelegateCommand(cancel);
  51. CheckedCommand = new DelegateCommand<string>(check);
  52. }
  53. private void check(string args)
  54. {
  55. switch (args)
  56. {
  57. case "Odd":
  58. PageList = new List<int>();
  59. for (int i = 1; i <= pageCount; i++)
  60. {
  61. if (i % 2 != 0)
  62. {
  63. PageList.Add(i - 1);
  64. }
  65. }
  66. break;
  67. case "Even":
  68. PageList = new List<int>();
  69. for (int i = 1; i <= pageCount; i++)
  70. {
  71. if (i % 2 == 0)
  72. {
  73. PageList.Add(i - 1);
  74. }
  75. }
  76. break;
  77. case "All":
  78. PageList = new List<int>();
  79. for (int i = 0; i < pageCount; i++)
  80. {
  81. PageList.Add(i);
  82. }
  83. break;
  84. case "Custom":
  85. if (!string.IsNullOrEmpty(InputString))
  86. {
  87. CheckCustomPage();
  88. }
  89. break;
  90. default:
  91. break;
  92. }
  93. }
  94. private void CheckCustomPage()
  95. {
  96. var result = CommonHelper.GetPagesInRange(ref PageList, InputString, pageCount, new char[] { ',' }, new char[] { '-' });
  97. if (!result)
  98. {
  99. AlertsMessage alertsMessage = new AlertsMessage();
  100. alertsMessage.ShowDialog("", "页码错误", "ok");
  101. InputString = "";
  102. return;
  103. }
  104. }
  105. private void ok()
  106. {
  107. DialogParameters valuePairs = new DialogParameters();
  108. valuePairs.Add(ParameterNames.PageList,PageList);
  109. RequestClose.Invoke(new DialogResult(ButtonResult.OK,valuePairs));
  110. }
  111. private void cancel()
  112. {
  113. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  114. }
  115. public bool CanCloseDialog()
  116. {
  117. return true;
  118. }
  119. public void OnDialogClosed()
  120. {
  121. }
  122. public void OnDialogOpened(IDialogParameters parameters)
  123. {
  124. PageCount = parameters.GetValue<int>(ParameterNames.PageCount);
  125. PageList = new List<int>();
  126. for (int i = 0; i < pageCount; i++)
  127. {
  128. PageList.Add(i);
  129. }
  130. }
  131. }
  132. }