InsertDialogViewModel.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using PDF_Office.Model;
  2. using PDF_Office.Model.PageEdit;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using Prism.Services.Dialogs;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
  14. {
  15. public class InsertDialogViewModel : BindableBase, IDialogAware
  16. {
  17. public string Title =>"";
  18. /// <summary>
  19. /// 数据模型
  20. /// </summary>
  21. private CustomInsertModel Model = new CustomInsertModel();
  22. public event Action<IDialogResult> RequestClose;
  23. private string currentPageSize;
  24. /// <summary>
  25. /// 当前页的尺寸大小 括号显示的形式
  26. /// </summary>
  27. public string CurrentPageSize
  28. {
  29. get { return currentPageSize; }
  30. set
  31. {
  32. SetProperty(ref currentPageSize, value);
  33. }
  34. }
  35. private int itemSelectedIndex = 0;
  36. /// <summary>
  37. /// 自定义页面的选中索引
  38. /// </summary>
  39. public int ItemSelectedIndex
  40. {
  41. get { return itemSelectedIndex; }
  42. set
  43. {
  44. SetProperty(ref itemSelectedIndex, value);
  45. if(Model!=null)
  46. {
  47. Model.filepath = Pages[itemSelectedIndex].FilePath;
  48. }
  49. }
  50. }
  51. private string customWidth;
  52. /// <summary>
  53. /// 自定义页面宽度
  54. /// </summary>
  55. public string CustomWidth
  56. {
  57. get { return customWidth; }
  58. set
  59. {
  60. SetProperty(ref customWidth, value);
  61. }
  62. }
  63. private string customHeight;
  64. /// <summary>
  65. /// 自定义页面高度
  66. /// </summary>
  67. public string CustomHeight
  68. {
  69. get { return customHeight; }
  70. set
  71. {
  72. SetProperty(ref customHeight, value);
  73. }
  74. }
  75. /// <summary>
  76. /// 自定义页面的路径集合
  77. /// </summary>
  78. public ObservableCollection<CustomPageItem> Pages { get; set; }
  79. /// <summary>
  80. /// 页面单位集合
  81. /// </summary>
  82. public List<string> Units { get; set; }
  83. public DelegateCommand CancelCommand { get; set; }
  84. public DelegateCommand InsertCommnad { get; set; }
  85. /// <summary>
  86. /// 页面方向选择的事件
  87. /// </summary>
  88. public DelegateCommand<string> OrientationCheckedCommand { get; set; }
  89. public InsertDialogViewModel()
  90. {
  91. InitPageSource();
  92. InitUnits();
  93. CancelCommand = new DelegateCommand(cancel);
  94. InsertCommnad = new DelegateCommand(insert);
  95. OrientationCheckedCommand = new DelegateCommand<string>(OrientationChecked);
  96. }
  97. /// <summary>
  98. /// 初始化页面大小单位集合
  99. /// </summary>
  100. private void InitUnits()
  101. {
  102. }
  103. /// <summary>
  104. /// 初始化自定义页面集合
  105. /// </summary>
  106. private void InitPageSource()
  107. {
  108. Pages = new ObservableCollection<CustomPageItem>();
  109. Pages.Add(new CustomPageItem() {Name="空白页",FilePath = ""});
  110. Pages.Add(new CustomPageItem() { Name="横线",FilePath= System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\HorizontalLine.png")});
  111. Pages.Add(new CustomPageItem() { Name = "五线谱", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\Staff.png") });
  112. Pages.Add(new CustomPageItem() { Name = "格子线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\GridLine.png") });
  113. }
  114. /// <summary>
  115. /// 页面方向选中事件
  116. /// </summary>
  117. /// <param name="orientation"></param>
  118. private void OrientationChecked(string orientation)
  119. {
  120. switch (orientation)
  121. {
  122. //如果宽高不符合条件就对调宽高
  123. case "Vertical":
  124. if(Model.height<=Model.width)
  125. {
  126. var temp = Model.height;
  127. Model.height = Model.width;
  128. Model.width = Model.height;
  129. }
  130. break;
  131. case "Horizontal":
  132. if (Model.height> Model.width)
  133. {
  134. var temp = Model.height;
  135. Model.height = Model.width;
  136. Model.width = Model.height;
  137. }
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. private void cancel()
  144. {
  145. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  146. }
  147. private void insert()
  148. {
  149. DialogParameters valuePairs = new DialogParameters();
  150. valuePairs.Add(ParameterNames.DataModel, Model);
  151. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  152. }
  153. #region 弹窗接口
  154. public bool CanCloseDialog()
  155. {
  156. return true;
  157. }
  158. public void OnDialogClosed()
  159. {
  160. }
  161. public void OnDialogOpened(IDialogParameters parameters)
  162. {
  163. if(parameters!=null)
  164. {
  165. var size = parameters.GetValue<Size>("CurrentPageSize");
  166. CurrentPageSize = $"({size.Width.ToString("F2")}mm*{size.Height.ToString("F2")}mm)";
  167. Model.width = (int)size.Width;
  168. Model.height = (int)size.Height;
  169. }
  170. }
  171. #endregion
  172. }
  173. }