Zxing.net.mobile: Cancel button for Xamarin.Forms version is missing

Created on 14 Mar 2017  路  5Comments  路  Source: Redth/ZXing.Net.Mobile

The Getting Started guide proposes a ZXingScannerPage. Now on iOS there is no back button and the default overlay doesn't provide a cancel button (only torch). The user isn't able to go back or to stop the scanner.

I didn't found any option for ZXingScannerPage. So should I use a DependencyService instead or how should I handle this case?

Most helpful comment

Here's another approach that does not use overlays. I wrapped my modal in a NavigationPage, then added a Cancel button as a ToolbarItem.
`

        var scannerPage = new ZXingScannerPage()
        {
            Title = "Scan QR Code"
        };

        scannerPage.OnScanResult += (result) =>
        {
            scannerPage.IsScanning = false;
            Device.BeginInvokeOnMainThread(async () =>
            {
                await Navigation.PopModalAsync();
                await DisplayAlert("Scanned", result.Text, "OK");
            });
        };

        var toolbarItem = new ToolbarItem { Text = "Cancel" };
        toolbarItem.Clicked += (s, e) =>
        {
            scannerPage.IsScanning = false;
            Device.BeginInvokeOnMainThread(async () =>
            {
                await Navigation.PopModalAsync();
            });
        };

        var navPage = new NavigationPage(scannerPage);
        navPage.ToolbarItems.Add(toolbarItem);

        await Navigation.PushModalAsync(navPage);

`

All 5 comments

create a custom overlay?

I also thought on that and I could add an abort button. But I only have a reference to ZXingScannerPage, which doesn't offer a Cancel() or StopScanning() method.

Now I'm using a custom overlay with an abort button:

var customOverlay = new StackLayout {
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand
};

var abort = new Button
{
    Text = "Abort",
};

abort.Clicked += Abort_Clicked;

customOverlay.Children.Add(abort);

this.scanPage = new ZXingScannerPage(customOverlay: customOverlay);

private void Abort_Clicked(object sender, EventArgs e)
{
    Device.BeginInvokeOnMainThread(async () =>
    {
        this.scanPage.IsScanning = false;
        await Navigation.PopModalAsync();
    });
}

To abort the operation I navigate back to dismiss the scanner page and abort the operation. Seems to work. Don't forget to do that on the UI thread.

Helpfull ......! Thanks

Here's another approach that does not use overlays. I wrapped my modal in a NavigationPage, then added a Cancel button as a ToolbarItem.
`

        var scannerPage = new ZXingScannerPage()
        {
            Title = "Scan QR Code"
        };

        scannerPage.OnScanResult += (result) =>
        {
            scannerPage.IsScanning = false;
            Device.BeginInvokeOnMainThread(async () =>
            {
                await Navigation.PopModalAsync();
                await DisplayAlert("Scanned", result.Text, "OK");
            });
        };

        var toolbarItem = new ToolbarItem { Text = "Cancel" };
        toolbarItem.Clicked += (s, e) =>
        {
            scannerPage.IsScanning = false;
            Device.BeginInvokeOnMainThread(async () =>
            {
                await Navigation.PopModalAsync();
            });
        };

        var navPage = new NavigationPage(scannerPage);
        navPage.ToolbarItems.Add(toolbarItem);

        await Navigation.PushModalAsync(navPage);

`

Was this page helpful?
0 / 5 - 0 ratings