123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using System.IO;
- using Syncfusion.XForms.SignaturePad;
- namespace PRS.Mobile
- {
- public delegate void NameAdded(string s);
- public delegate void NameRemoved(string s);
- class SignaturePad : Frame
- {
- private SfSignaturePad Pad = new SfSignaturePad();
- public string Name = "";
- public string Data = "";
- public bool StrokeAdded = false;
- public event NameAdded OnNameAdded;
- public event NameRemoved OnNameRemoved;
- private Entry entry = new Entry();
- Grid SignatureGrid = new Grid() { Margin = 0, Padding = 0 };
- EmbeddedImageCapture hiddenEmbeddedImageCapture = new EmbeddedImageCapture() { HeightRequest = 180, IsVisible = false, IsEnabled = false };
- public SignaturePad()
- {
- Padding = 4;
- Pad = new SfSignaturePad();
- Pad.BackgroundColor = Color.White;
- Pad.MinimumHeightRequest = 200;
- Pad.MinimumStrokeWidth = 0.2;
- Pad.MaximumStrokeWidth = 17;
- Pad.StrokeCompleted += Pad_StrokeCompleted;
- entry = new Entry
- {
- Placeholder = "Add Name",
- HorizontalOptions = LayoutOptions.FillAndExpand,
- Margin = new Thickness(1, 1, 0, 1),
- BackgroundColor = Color.White,
- VerticalOptions = LayoutOptions.Center
- };
- entry.TextChanged += (object sender, TextChangedEventArgs e) =>
- {
- entry.Text = entry.Text.ToUpper().Trim();
- Name = entry.Text;
- };
- Button lookupButton = new Button()
- {
- Text = "...",
- VerticalOptions = LayoutOptions.Center,
- FontAttributes = FontAttributes.Bold,
- CornerRadius = 5,
- Padding = 1,
- Margin = new Thickness(0, 1, 0, 1),
- };
- lookupButton.Clicked += (object sender, EventArgs e) =>
- {
- EmployeeSelectionPage page = new EmployeeSelectionPage(
- (employee) =>
- {
- entry.Text = employee.Name.ToUpper();
- OnNameAdded?.Invoke(entry.Text);
- });
- Navigation.PushAsync(page);
- };
- Button clearButton = new Button()
- {
- Text = "Clear",
- HorizontalOptions = LayoutOptions.FillAndExpand,
- VerticalOptions = LayoutOptions.End,
- Margin = new Thickness(0, 1, 1, 1),
- BackgroundColor = Color.FromHex("#15C7C1"),
- FontAttributes = FontAttributes.Bold,
- TextColor = Color.White,
- CornerRadius = 5,
- Padding = 1
- };
- clearButton.Clicked += (object sender, EventArgs e) =>
- {
- Pad.Clear();
- hiddenEmbeddedImageCapture.ClearItems();
- hiddenEmbeddedImageCapture.IsVisible = false;
- Pad.IsVisible = true;
- ClearName();
- Data = "";
- StrokeAdded = false;
- };
- Grid.SetColumn(entry, 0);
- Grid.SetColumn(lookupButton, 1);
- Grid.SetColumn(clearButton, 2);
- Grid buttonsGrid = new Grid { Margin = 0, Padding = 0, VerticalOptions = LayoutOptions.End };
- buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
- buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(35, GridUnitType.Absolute) });
- buttonsGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
- buttonsGrid.Children.Add(entry);
- buttonsGrid.Children.Add(lookupButton);
- buttonsGrid.Children.Add(clearButton);
-
- SignatureGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(200, GridUnitType.Absolute) });
- Grid.SetRow(Pad, 0);
- Grid.SetRow(hiddenEmbeddedImageCapture, 0);
- SignatureGrid.Children.Add(hiddenEmbeddedImageCapture);
- SignatureGrid.Children.Add(Pad);
- SignatureGrid.Children.Add(buttonsGrid);
- CornerRadius = 10;
- BorderColor = Color.FromHex("#15C7C1");
- Content = SignatureGrid;
- }
- private void Pad_StrokeCompleted(object sender, EventArgs e)
- {
- StrokeAdded = true;
- }
- public void ClearName()
- {
- OnNameRemoved?.Invoke(entry.Text);
- entry.Text = "";
- }
- public void SaveSignature()
- {
- if (Data == null || string.IsNullOrWhiteSpace(Data))
- {
- Pad.Save();
- if (Pad.ImageSource != null)
- {
- Data = ImageSourceToBase64(Pad.ImageSource);
- }
- }
- }
- public void PopulateSignature(byte[] data)
- {
- Pad.IsVisible = false;
- hiddenEmbeddedImageCapture.IsVisible = true;
- hiddenEmbeddedImageCapture.DataToImage(data);
- ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
- Data = ImageSourceToBase64(src);
- StrokeAdded = true;
- }
- public void PopulateName(string s)
- {
- entry.Text = s;
- }
- private string ImageSourceToBase64(ImageSource source)
- {
- StreamImageSource streamImageSource = (StreamImageSource)source;
- System.Threading.CancellationToken cancellationToken = System.Threading.CancellationToken.None;
- Task<Stream> task = streamImageSource.Stream(cancellationToken);
- Stream stream = task.Result;
- byte[] bytes = new byte[stream.Length];
- stream.Read(bytes, 0, bytes.Length);
- string s = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks);
- return s;
- }
- }
- }
|