forked from Redth/ZXing.Net.Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZXingBarcodeImageViewRenderer.uwp.cs
84 lines (73 loc) · 2.23 KB
/
ZXingBarcodeImageViewRenderer.uwp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using Xamarin.Forms;
using ZXing.Net.Mobile.Forms;
using ZXing.Net.Mobile.Forms.WindowsUniversal;
using Xamarin.Forms.Platform.UWP;
using System.ComponentModel;
using System.Reflection;
using ZXing.Mobile;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;
[assembly: ExportRenderer(typeof(ZXingBarcodeImageView), typeof(ZXingBarcodeImageViewRenderer))]
namespace ZXing.Net.Mobile.Forms.WindowsUniversal
{
[Preserve]
public class ZXingBarcodeImageViewRenderer : ViewRenderer<ZXingBarcodeImageView, Windows.UI.Xaml.Controls.Image>
{
public static void Init()
{
var tmp = DateTime.UtcNow;
}
ZXingBarcodeImageView formsView;
Windows.UI.Xaml.Controls.Image imageView;
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ZXingBarcodeImageView.BarcodeValue)
|| e.PropertyName == nameof(ZXingBarcodeImageView.BarcodeOptions)
|| e.PropertyName == nameof(ZXingBarcodeImageView.BarcodeFormat))
Regenerate();
base.OnElementPropertyChanged(sender, e);
}
protected override void OnElementChanged(ElementChangedEventArgs<ZXingBarcodeImageView> e)
{
formsView = Element;
if (formsView != null && imageView == null)
{
imageView = new Windows.UI.Xaml.Controls.Image();
SetNativeControl(imageView);
}
Regenerate();
base.OnElementChanged(e);
}
void Regenerate()
{
BarcodeWriter writer = null;
string barcodeValue = null;
if (formsView != null
&& imageView != null
&& !string.IsNullOrWhiteSpace(formsView.BarcodeValue)
&& formsView.BarcodeFormat != BarcodeFormat.All_1D)
{
writer = new BarcodeWriter { Format = formsView.BarcodeFormat };
if (formsView.BarcodeOptions != null)
writer.Options = formsView.BarcodeOptions;
barcodeValue = formsView.BarcodeValue;
}
// Update or clear out the image depending if we had enough info
// to instantiate the barcode writer, otherwise null the image
Device.BeginInvokeOnMainThread(() =>
{
try
{
var image = writer?.Write(barcodeValue);
if (imageView != null)
imageView.Source = image;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to update image: {ex}");
}
});
}
}
}