im trying to implement a Barcode Scanner on my project in Xamarin Forms. When i press the back button on the scanning page my app crash. I am getting the following error: System.NullReferenceException: 'Object reference not set to an instance of an object.'
This is my code:
ScanService.cs
public interface ScanningService
{
Task<string> ScanAsync();
}
My code behind:
var scanner = DependencyService.Get<ScanningService>();
var result = await scanner.ScanAsync();
if (result != null){.....}
QRscanningService (On Android):
class QrScanningService : ScanningService
{
public async Task<string> ScanAsync()
{
var optionsCustom = new MobileBarcodeScanningOptions();
var scanner = new MobileBarcodeScanner()
{
TopText = "Acerca la cámara",
BottomText = "Toca la pantalla para enfocar",
};
var scanResult = await scanner.Scan(optionsCustom);
return scanResult.Text;
}
}
Any contribution will be apreciated.