forked from rebuy-de/rb-forms-barcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScannerPage.xaml.cs
88 lines (72 loc) · 2.3 KB
/
ScannerPage.xaml.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
85
86
87
using Xamarin.Forms;
using Rb.Forms.Barcode.Pcl;
using System;
namespace Sample.Pcl.Pages
{
public partial class ScannerPage : ContentPage
{
public ScannerPage()
{
InitializeComponent();
NavigationPage.SetHasBackButton(this, false);
/**
* So that we can release the camera when turning off phone or switching apps.
*/
MessagingCenter.Subscribe<App>(this, App.MessageOnSleep, disableScanner);
MessagingCenter.Subscribe<App>(this, App.MessageOnResume, enableScanner);
barcodeScanner.BarcodeChanged += animateFlash;
}
protected override void OnAppearing()
{
barcodeScanner.IsEnabled = true;
base.OnAppearing();
}
protected override void OnDisappearing()
{
barcodeScanner.IsEnabled = false;
base.OnDisappearing();
}
public void DisableScanner()
{
disableScanner(null);
}
private void gotoThirdPage(Object sender, EventArgs e)
{
Navigation.PushAsync(new ThirdPage());
}
/**
* Release camera so that other apps can access it.
*/
private void disableScanner(object sender)
{
barcodeScanner.IsEnabled = false;
}
/**
* All your camera belongs to us.
*/
private void enableScanner(object sender)
{
barcodeScanner.IsEnabled = true;
}
private async void animateFlash(object sender, BarcodeEventArgs e)
{
Device.BeginInvokeOnMainThread(async () => {
await flash.FadeTo(1, 150, Easing.CubicInOut);
flash.Opacity = 0;
});
}
/**
* You need to take care of realeasing the camera when you are done with it else bad things can happen!
*/
~ScannerPage()
{
disableScanner(this);
/**
* Camera is released we dont need the events anymore.
*/
MessagingCenter.Unsubscribe<App>(this, App.MessageOnSleep);
MessagingCenter.Unsubscribe<App>(this, App.MessageOnResume);
barcodeScanner.BarcodeChanged -= animateFlash;
}
}
}