AddExistedCertificationControl.xaml.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using ComPDFKit.DigitalSign;
  2. using Compdfkit_Tools.Helper;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Runtime.CompilerServices;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. namespace Compdfkit_Tools.PDFControl
  9. {
  10. public partial class AddExistedCertificationControl : UserControl,INotifyPropertyChanged
  11. {
  12. public event EventHandler SaveEvent;
  13. public event EventHandler CancelEvent;
  14. public event EventHandler<CertificateAccess> FillSignatureEvent;
  15. public event PropertyChangedEventHandler PropertyChanged;
  16. private bool _canContinue;
  17. public bool CanContinue
  18. {
  19. get => _canContinue;
  20. set => UpdateProper(ref _canContinue, value);
  21. }
  22. public AddExistedCertificationControl()
  23. {
  24. InitializeComponent();
  25. DataContext = this;
  26. }
  27. private void CancelBtn_Click(object sender, System.Windows.RoutedEventArgs e)
  28. {
  29. CancelEvent?.Invoke(this, EventArgs.Empty);
  30. }
  31. private void DoneBtn_Click(object sender, System.Windows.RoutedEventArgs e)
  32. {
  33. if (!CPDFPKCS12CertHelper.CheckPKCS12Password(FileNameTxt.Text, PasswordBoxTxt.Password))
  34. {
  35. ErrorTipsText.Text = "Invalid Password.";
  36. return;
  37. }
  38. FillSignatureEvent?.Invoke(sender, new CertificateAccess { filePath = FileNameTxt.Text, password = PasswordBoxTxt.Password });
  39. }
  40. private void SelectFileBtn_Click(object sender, System.Windows.RoutedEventArgs e)
  41. {
  42. string filePath = CommonHelper.GetExistedPathOrEmpty("PFX Files(*.pfx) | *.pfx|P12 Files(*.p12) | *.p12");
  43. if (filePath != string.Empty)
  44. {
  45. FileNameTxt.Text = filePath;
  46. }
  47. }
  48. private void PasswordBoxTxt_OnPasswordChanged(object sender, RoutedEventArgs e)
  49. {
  50. PasswordBox passwordBox = (PasswordBox)sender;
  51. if (passwordBox.Password.Length > 0)
  52. {
  53. PasswordTextBlock.Visibility = Visibility.Hidden;
  54. }
  55. else
  56. {
  57. PasswordTextBlock.Visibility = Visibility.Visible;
  58. }
  59. CanContinue = FileNameTxt.Text.Length > 0 && PasswordBoxTxt.Password.Length > 0;
  60. }
  61. private void FileNameTxt_OnTextChanged(object sender, TextChangedEventArgs e)
  62. {
  63. CanContinue = FileNameTxt.Text.Length > 0 && PasswordBoxTxt.Password.Length > 0;
  64. }
  65. protected void UpdateProper<T>(ref T properValue,
  66. T newValue,
  67. [CallerMemberName] string properName = "")
  68. {
  69. if (object.Equals(properValue, newValue))
  70. return;
  71. properValue = newValue;
  72. OnPropertyChanged(properName);
  73. }
  74. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  75. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  76. }
  77. }