|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using Xamarin.Forms; |
| 4 | +using Xamarin.Forms.Platform.GTK; |
| 5 | +using ZXing.Net.Mobile.Forms; |
| 6 | +using ZXing.Net.Mobile.Forms.GTK; |
| 7 | +using ZXing.Net.Mobile.GTK; |
| 8 | +using Image = Gtk.Image; |
| 9 | + |
| 10 | +[assembly:ExportRenderer(typeof(ZXingBarcodeImageView), typeof(ZXingBarcodeImageViewRenderer))] |
| 11 | +namespace ZXing.Net.Mobile.Forms.GTK |
| 12 | +{ |
| 13 | + public class ZXingBarcodeImageViewRenderer : ViewRenderer<ZXingBarcodeImageView, Image> |
| 14 | + { |
| 15 | + public static void Init() |
| 16 | + { |
| 17 | + var temp = DateTime.Now; |
| 18 | + } |
| 19 | + |
| 20 | + ZXingBarcodeImageView formsView; |
| 21 | + Image gtkImage; |
| 22 | + |
| 23 | + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) |
| 24 | + { |
| 25 | + // in GTK there are a way to many properties that are changed compared to other platforms |
| 26 | + if (e.PropertyName == ZXingBarcodeImageView.BarcodeValueProperty.PropertyName || |
| 27 | + e.PropertyName == ZXingBarcodeImageView.BarcodeFormatProperty.PropertyName || |
| 28 | + e.PropertyName == ZXingBarcodeImageView.BarcodeOptionsProperty.PropertyName) |
| 29 | + { |
| 30 | + Regenerate(); |
| 31 | + } |
| 32 | + |
| 33 | + base.OnElementPropertyChanged(sender, e); |
| 34 | + } |
| 35 | + |
| 36 | + protected override void OnElementChanged(ElementChangedEventArgs<ZXingBarcodeImageView> e) |
| 37 | + { |
| 38 | + formsView = Element; |
| 39 | + |
| 40 | + if (gtkImage == null) |
| 41 | + { |
| 42 | + gtkImage = new Image(); |
| 43 | + |
| 44 | + base.SetNativeControl(gtkImage); |
| 45 | + } |
| 46 | + |
| 47 | + Regenerate(); |
| 48 | + |
| 49 | + base.OnElementChanged(e); |
| 50 | + } |
| 51 | + |
| 52 | + void Regenerate () |
| 53 | + { |
| 54 | + if (formsView != null && formsView.BarcodeValue != null) |
| 55 | + { |
| 56 | + var writer = new BarcodeWriter(); |
| 57 | + |
| 58 | + if (formsView != null && formsView.BarcodeOptions != null) |
| 59 | + writer.Options = formsView.BarcodeOptions; |
| 60 | + if (formsView != null && formsView.BarcodeFormat != null) |
| 61 | + writer.Format = formsView.BarcodeFormat; |
| 62 | + |
| 63 | + var value = formsView != null ? formsView.BarcodeValue : string.Empty; |
| 64 | + |
| 65 | + Device.BeginInvokeOnMainThread(() => |
| 66 | + { |
| 67 | + var pixBuf = writer.Write(value); |
| 68 | + gtkImage.Pixbuf = pixBuf; |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments