SignatureEditor.xaml.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8. using Xamarin.Forms.Xaml;
  9. namespace PRS.Mobile
  10. {
  11. public class SignatureEditorChangedEventArgs : EventArgs
  12. {
  13. }
  14. public delegate void SignatureEditorChangedEvent(object sender, SignatureEditorChangedEventArgs args);
  15. [XamlCompilation(XamlCompilationOptions.Compile)]
  16. public partial class SignatureEditor
  17. {
  18. public event SignatureEditorChangedEvent Changed;
  19. public void Load(byte[] data)
  20. {
  21. if (data?.Any() == true)
  22. {
  23. signaturePad.IsVisible = false;
  24. _preview.Source = ImageSource.FromStream(() => { return new MemoryStream(data); });
  25. _preview.IsVisible = true;
  26. }
  27. else
  28. {
  29. _preview.IsVisible = false;
  30. _preview.Source = null;
  31. signaturePad.IsVisible = true;
  32. signaturePad.Clear();
  33. }
  34. }
  35. public byte[] Save()
  36. {
  37. signaturePad.Save();
  38. if (signaturePad.ImageSource is StreamImageSource source)
  39. {
  40. Task<Stream> task = source.Stream(System.Threading.CancellationToken.None);
  41. Stream stream = task.Result;
  42. byte[] bytes = new byte[stream.Length];
  43. stream.Read(bytes, 0, bytes.Length);
  44. Load(bytes);
  45. return bytes;
  46. }
  47. return new byte[] { };
  48. }
  49. public SignatureEditor()
  50. {
  51. InitializeComponent();
  52. Load(null);
  53. }
  54. private void SignaturePad_OnStrokeCompleted(object sender, EventArgs e)
  55. {
  56. Changed?.Invoke(this,new SignatureEditorChangedEventArgs());
  57. }
  58. }
  59. }