VerifyPassWordDialogViewModel.cs 2.4 KB

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