VerifyPassWordDialogViewModel.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using ComPDFKit.PDFDocument;
  2. using PDF_Master.Model;
  3. using Prism.Commands;
  4. using Prism.Mvvm;
  5. using Prism.Services.Dialogs;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. namespace PDF_Master.ViewModels.Dialog
  13. {
  14. public class VerifyPassWordDialogViewModel : BindableBase, IDialogAware
  15. {
  16. public string Title => "VerifyPassWordDialog";
  17. public string WarningText => "Error PassWord";
  18. public event Action<IDialogResult> RequestClose;
  19. public DelegateCommand OKCommand { get; set; }
  20. public DelegateCommand CancelCommand { get; set; }
  21. private string input;
  22. public string Input
  23. {
  24. get { return input; }
  25. set
  26. {
  27. SetProperty(ref input, value);
  28. ShowWarning = Visibility.Collapsed;
  29. }
  30. }
  31. private Visibility showWarning = Visibility.Collapsed;
  32. public Visibility ShowWarning
  33. {
  34. get { return showWarning; }
  35. set
  36. {
  37. SetProperty(ref showWarning, value);
  38. }
  39. }
  40. private CPDFDocument document;
  41. public VerifyPassWordDialogViewModel()
  42. {
  43. OKCommand = new DelegateCommand(OK);
  44. CancelCommand = new DelegateCommand(Cancel);
  45. }
  46. private void OK()
  47. {
  48. if (string.IsNullOrEmpty(input))
  49. return;
  50. bool result = document.UnlockWithPassword(input.Trim());
  51. if (!result)
  52. {
  53. ShowWarning = Visibility.Visible;
  54. return;
  55. }
  56. var dialogresult = new DialogResult(ButtonResult.OK);
  57. dialogresult.Parameters.Add(ParameterNames.PassWord, input.Trim());
  58. RequestClose.Invoke(dialogresult);
  59. }
  60. private void Cancel()
  61. {
  62. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  63. }
  64. public bool CanCloseDialog()
  65. {
  66. return true;
  67. }
  68. public void OnDialogClosed()
  69. {
  70. //引用类型的Doc对象 不能释放,会导致报错
  71. }
  72. public void OnDialogOpened(IDialogParameters parameters)
  73. {
  74. CPDFDocument doc = null;
  75. parameters.TryGetValue<CPDFDocument>(ParameterNames.PDFDocument, out doc);
  76. if (doc != null)
  77. {
  78. document = doc;
  79. }
  80. }
  81. }
  82. }