CreateFromHtmlDialogViewModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using Microsoft.Win32;
  2. using PDF_Master.Model;
  3. using PDF_Master.Model.Dialog.HomePageToolsDialogs;
  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.HomePageToolsDialogs
  13. {
  14. public class CreateFromHtmlDialogViewModel : BindableBase, IDialogAware
  15. {
  16. public string Title => "";
  17. public event Action<IDialogResult> RequestClose;
  18. private string filepath = "";
  19. public string FilePath
  20. {
  21. get { return filepath; }
  22. set
  23. {
  24. SetProperty(ref filepath, value);
  25. if(!string.IsNullOrEmpty(value))
  26. {
  27. Model.FilePath = value;
  28. }
  29. }
  30. }
  31. private double margin = 0;
  32. public double Margin
  33. {
  34. get { return margin; }
  35. set
  36. {
  37. SetProperty(ref margin, value);
  38. Model.Margin = value;
  39. }
  40. }
  41. private int selectedIndex = 0;
  42. public int SelectedIndex
  43. {
  44. get { return selectedIndex; }
  45. set
  46. {
  47. SetProperty(ref selectedIndex, value);
  48. if (value>0)
  49. {
  50. Model.PageSize = (Microsoft.Office.Interop.Word.WdPaperSize)value;
  51. }
  52. }
  53. }
  54. public List<string> PageSizes { get; set; } = new List<string>();
  55. /// <summary>
  56. /// 数据模型
  57. /// </summary>
  58. public HtmlModel Model { get; set; } = new HtmlModel();
  59. public DelegateCommand OpenFileCommnad { get; set; }
  60. public DelegateCommand CreateCommand { get; set; }
  61. public DelegateCommand CancelCommand { get; set; }
  62. public CreateFromHtmlDialogViewModel()
  63. {
  64. OpenFileCommnad = new DelegateCommand(openFile);
  65. CreateCommand = new DelegateCommand(createFile,CanExcuteCreateFile).ObservesProperty(()=> FilePath);
  66. CancelCommand = new DelegateCommand(cancel);
  67. InitPageSize();
  68. }
  69. private void InitPageSize()
  70. {
  71. PageSizes.Add("10x14");
  72. PageSizes.Add("11x17");
  73. PageSizes.Add("Letter");
  74. PageSizes.Add("LetterSmall");
  75. PageSizes.Add("Legal");
  76. PageSizes.Add("Executive");
  77. PageSizes.Add("A3");
  78. PageSizes.Add("A4");
  79. PageSizes.Add("A4Small");
  80. PageSizes.Add("A5");
  81. PageSizes.Add("B4");
  82. PageSizes.Add("B5");
  83. PageSizes.Add("CSheet");
  84. PageSizes.Add("DSheet");
  85. PageSizes.Add("ESheet");
  86. PageSizes.Add("FanfoldLegalGerman");
  87. PageSizes.Add("FanfoldStdGerman");
  88. PageSizes.Add("FanfoldUS");
  89. PageSizes.Add("Folio");
  90. PageSizes.Add("Ledger");
  91. PageSizes.Add("Note");
  92. PageSizes.Add("Quarto");
  93. PageSizes.Add("Statement");
  94. PageSizes.Add("Tabloid");
  95. PageSizes.Add("Envelope9");
  96. PageSizes.Add("Envelope10");
  97. PageSizes.Add("Envelope11");
  98. PageSizes.Add("Envelope12");
  99. PageSizes.Add("EnvelopeB4");
  100. PageSizes.Add("EnvelopeB5");
  101. PageSizes.Add("EnvelopeB6");
  102. PageSizes.Add("EnvelopeC3");
  103. PageSizes.Add("EnvelopeC4");
  104. PageSizes.Add("EnvelopeC6");
  105. PageSizes.Add("EnvelopeC65");
  106. PageSizes.Add("EnvelopeDL");
  107. PageSizes.Add("EnvelopeItaly");
  108. PageSizes.Add("EnvelopeMonarch");
  109. PageSizes.Add("EnvelopePersonal");
  110. }
  111. private void createFile()
  112. {
  113. DialogParameters valuePairs = new DialogParameters();
  114. valuePairs.Add(ParameterNames.DataModel, Model);
  115. RequestClose.Invoke(new DialogResult(ButtonResult.OK,valuePairs));
  116. }
  117. /// <summary>
  118. /// 是否可以创建
  119. /// </summary>
  120. /// <returns></returns>
  121. private bool CanExcuteCreateFile()
  122. {
  123. return !string.IsNullOrEmpty(FilePath);
  124. }
  125. private void cancel()
  126. {
  127. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  128. }
  129. private void openFile()
  130. {
  131. OpenFileDialog dialog = new OpenFileDialog();
  132. dialog.Multiselect = false;
  133. dialog.Filter = "HTML(*.html)|*.html";
  134. if((bool)dialog.ShowDialog())
  135. {
  136. if(!string.IsNullOrEmpty(dialog.FileName))
  137. {
  138. FilePath = dialog.FileName;
  139. }
  140. }
  141. }
  142. #region 框架
  143. public bool CanCloseDialog()
  144. {
  145. return true;
  146. }
  147. public void OnDialogClosed()
  148. {
  149. }
  150. public void OnDialogOpened(IDialogParameters parameters)
  151. {
  152. }
  153. #endregion
  154. }
  155. }