InsertDialogViewModel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. private int unitsSelectedIndex = 0;
  76. /// <summary>
  77. /// 单位下拉框的选中项索引
  78. /// </summary>
  79. public int UnitsSelectedIndex
  80. {
  81. get { return unitsSelectedIndex; }
  82. set
  83. {
  84. SetProperty(ref unitsSelectedIndex, value);
  85. }
  86. }
  87. /// <summary>
  88. /// 自定义页面的路径集合
  89. /// </summary>
  90. public ObservableCollection<CustomPageItem> Pages { get; set; }
  91. /// <summary>
  92. /// 页面单位集合
  93. /// </summary>
  94. public List<string> Units { get; set; }
  95. public DelegateCommand CancelCommand { get; set; }
  96. public DelegateCommand InsertCommnad { get; set; }
  97. /// <summary>
  98. /// 页面方向选择的事件
  99. /// </summary>
  100. public DelegateCommand<string> OrientationCheckedCommand { get; set; }
  101. public InsertDialogViewModel()
  102. {
  103. InitPageSource();
  104. InitUnits();
  105. CancelCommand = new DelegateCommand(cancel);
  106. InsertCommnad = new DelegateCommand(insert);
  107. OrientationCheckedCommand = new DelegateCommand<string>(OrientationChecked);
  108. }
  109. /// <summary>
  110. /// 初始化页面大小单位集合
  111. /// </summary>
  112. private void InitUnits()
  113. {
  114. Units = new List<string>();
  115. Units.Add("mm");
  116. Units.Add("cm");
  117. Units.Add("in");
  118. }
  119. /// <summary>
  120. /// 初始化自定义页面集合
  121. /// </summary>
  122. private void InitPageSource()
  123. {
  124. Pages = new ObservableCollection<CustomPageItem>();
  125. Pages.Add(new CustomPageItem() {Name="空白页",FilePath = ""});
  126. Pages.Add(new CustomPageItem() { Name="横线",FilePath= System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\HorizontalLine.jpg")});
  127. Pages.Add(new CustomPageItem() { Name = "五线谱", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\Staff.jpg") });
  128. Pages.Add(new CustomPageItem() { Name = "格子线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\GridLine.jpg") });
  129. }
  130. /// <summary>
  131. /// 页面方向选中事件
  132. /// </summary>
  133. /// <param name="orientation"></param>
  134. private void OrientationChecked(string orientation)
  135. {
  136. switch (orientation)
  137. {
  138. //如果宽高不符合条件就对调宽高
  139. case "Vertical":
  140. if(Model.height<=Model.width)
  141. {
  142. var temp = Model.height;
  143. Model.height = Model.width;
  144. Model.width = Model.height;
  145. }
  146. break;
  147. case "Horizontal":
  148. if (Model.height> Model.width)
  149. {
  150. var temp = Model.height;
  151. Model.height = Model.width;
  152. Model.width = Model.height;
  153. }
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. private void cancel()
  160. {
  161. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  162. }
  163. private void insert()
  164. {
  165. DialogParameters valuePairs = new DialogParameters();
  166. valuePairs.Add(ParameterNames.DataModel, Model);
  167. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  168. }
  169. #region 弹窗接口
  170. public bool CanCloseDialog()
  171. {
  172. return true;
  173. }
  174. public void OnDialogClosed()
  175. {
  176. }
  177. public void OnDialogOpened(IDialogParameters parameters)
  178. {
  179. if(parameters!=null)
  180. {
  181. var size = parameters.GetValue<Size>("CurrentPageSize");
  182. CurrentPageSize = $"({size.Width.ToString("F2")}mm*{size.Height.ToString("F2")}mm)";
  183. CustomWidth = size.Width.ToString("F2");
  184. CustomHeight = size.Height.ToString("F2");
  185. Model.width = (int)size.Width;
  186. Model.height = (int)size.Height;
  187. }
  188. }
  189. #endregion
  190. }
  191. }