PageMarkDialogViewModel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 PageMarkDialogViewModel : 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. /// <summary>
  22. /// 当前页
  23. /// </summary>
  24. public int CurrentPageIndex { get; set; }
  25. private string pagecount = "/1";
  26. /// <summary>
  27. /// 页面总数
  28. /// </summary>
  29. public string PageCount
  30. {
  31. get { return pagecount; }
  32. set
  33. {
  34. SetProperty(ref pagecount, value);
  35. }
  36. }
  37. /// <summary>
  38. /// 页码集合
  39. /// </summary>
  40. public List<int> PageList = new List<int>();
  41. /// <summary>
  42. /// 文档页面总数
  43. /// </summary>
  44. public int pageCount = 0;
  45. private string custompage;
  46. /// <summary>
  47. /// 自定义页面
  48. /// </summary>
  49. public string CustomPage
  50. {
  51. get { return custompage; }
  52. set
  53. {
  54. SetProperty(ref custompage, value);
  55. if(!string.IsNullOrEmpty(value))
  56. {
  57. CheckCustomPage();
  58. }
  59. }
  60. }
  61. public PageMarkDialogViewModel()
  62. {
  63. OkCommand = new DelegateCommand(ok);
  64. CancelCommand = new DelegateCommand(cancel);
  65. CheckedCommand = new DelegateCommand<string>(check);
  66. }
  67. private void cancel()
  68. {
  69. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  70. }
  71. private void ok()
  72. {
  73. DialogParameters valuePairs = new DialogParameters();
  74. valuePairs.Add(ParameterNames.PageList, PageList);
  75. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  76. }
  77. private void CheckCustomPage()
  78. {
  79. var result = CommonHelper.GetPagesInRange(ref PageList,CustomPage,pageCount,new char[]{ ','},new char[] { '-'});
  80. if(!result)
  81. {
  82. AlertsMessage alertsMessage = new AlertsMessage();
  83. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  84. CustomPage = "";
  85. return;
  86. }
  87. }
  88. private void check(string args)
  89. {
  90. switch (args)
  91. {
  92. case "Odd":
  93. PageList = new List<int>();
  94. for (int i = 1; i <=pageCount; i++)
  95. {
  96. if (i % 2 != 0)
  97. {
  98. PageList.Add(i-1);
  99. }
  100. }
  101. break;
  102. case "Even":
  103. PageList = new List<int>();
  104. for (int i = 1; i <= pageCount; i++)
  105. {
  106. if (i % 2 == 0)
  107. {
  108. PageList.Add(i - 1);
  109. }
  110. }
  111. break;
  112. case "All":
  113. PageList = new List<int>();
  114. for(int i=0;i<pageCount;i++)
  115. {
  116. PageList.Add(i);
  117. }
  118. break;
  119. case "Custom":
  120. if(!string.IsNullOrEmpty(CustomPage))
  121. {
  122. CheckCustomPage();
  123. }
  124. break;
  125. default:
  126. case "Current":
  127. PageList = new List<int>();
  128. PageList.Add(CurrentPageIndex);
  129. break;
  130. }
  131. }
  132. public bool CanCloseDialog()
  133. {
  134. return true;
  135. }
  136. public void OnDialogClosed()
  137. {
  138. }
  139. public void OnDialogOpened(IDialogParameters parameters)
  140. {
  141. int page = parameters.GetValue<int>(ParameterNames.PageCount);
  142. if(page>0)
  143. {
  144. PageCount = "/ " + page;
  145. pageCount = page;
  146. }
  147. PageList = new List<int>();
  148. for (int i = 0; i < pageCount; i++)
  149. {
  150. PageList.Add(i);
  151. }
  152. CurrentPageIndex = parameters.GetValue<int>(ParameterNames.CurrentPageIndex);
  153. }
  154. }
  155. }