简体   繁体   中英

Can't implement the code-behind from the README file correctly

I'm using Xamarin on my mac. My plan is to make an app that can scan QR codes and store the information for later use. I've been spending lots of hours on udemy taking a Xamarin course as well as ac# course. I installed this package "ZXing.Net.Mobile.Forms" thinking it would make my life easier. I added the codes from the readme file in my code-behind but there are some issues with it. Did I apply them not correctly?

I would be glad if someone could help me out or give me some tips to get my app working.

Ps: It seems like the function wants me to actually press a button to scan. Isn't it possible to start scanning as soon as the app opens and only stop scanning when it actually found something or the app is closed?

Here is my code behind

在此处输入图片说明

Are you using XamarinForms for your application? If Yes I think you can't use MobileBarcodeScanner method. You should use ZXingScannerPage();

here a sample

public class ScanPage : ContentPage
{
    ZXingScannerPage scanPage;
    Button buttonScanDefaultOverlay;
    Button buttonScanCustomOverlay;
    Button buttonScanContinuously;
    Button buttonScanCustomPage;
    Button buttonGenerateBarcode;

    public ScanPage() : base()
    {
        buttonScanDefaultOverlay = new Button
        {
            Text = "Scan with Default Overlay",
            AutomationId = "scanWithDefaultOverlay",
        };
        buttonScanDefaultOverlay.Clicked += async delegate {
            scanPage = new ZXingScannerPage();
            scanPage.OnScanResult += (result) => {
                scanPage.IsScanning = false;

                Device.BeginInvokeOnMainThread(() => {
                    Navigation.PopAsync();
                    DisplayAlert("Scanned Barcode", result.Text, "OK");
                });
            };

            await Navigation.PushAsync(scanPage);
        };


        buttonScanCustomOverlay = new Button
        {
            Text = "Scan with Custom Overlay",
            AutomationId = "scanWithCustomOverlay",
        };
        buttonScanCustomOverlay.Clicked += async delegate {
            // Create our custom overlay
            var customOverlay = new StackLayout
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand
            };
            var torch = new Button
            {
                Text = "Toggle Torch"
            };
            torch.Clicked += delegate {
                scanPage.ToggleTorch();
            };
            customOverlay.Children.Add(torch);

            scanPage = new ZXingScannerPage(customOverlay: customOverlay);
            scanPage.OnScanResult += (result) => {
                scanPage.IsScanning = false;

                Device.BeginInvokeOnMainThread(() =>
                {
                    Navigation.PopAsync();
                    DisplayAlert("Scanned Barcode", result.Text, "OK");
                });
            };
            await Navigation.PushAsync(scanPage);
        };


        buttonScanContinuously = new Button
        {
            Text = "Scan Continuously",
            AutomationId = "scanContinuously",
        };
        buttonScanContinuously.Clicked += async delegate {
            scanPage = new ZXingScannerPage();
            scanPage.OnScanResult += (result) =>
                Device.BeginInvokeOnMainThread(() =>
                   DisplayAlert("Scanned Barcode", result.Text, "OK"));

            await Navigation.PushAsync(scanPage);
        };

        buttonScanCustomPage = new Button
        {
            Text = "Scan with Custom Page",
            AutomationId = "scanWithCustomPage",
        };


        buttonGenerateBarcode = new Button
        {
            Text = "Barcode Generator",
            AutomationId = "barcodeGenerator",
        };

        var stack = new StackLayout();
        stack.Children.Add(buttonScanDefaultOverlay);
        stack.Children.Add(buttonScanCustomOverlay);
        stack.Children.Add(buttonScanContinuously);
        stack.Children.Add(buttonScanCustomPage);
        stack.Children.Add(buttonGenerateBarcode);

        Content = stack;
    }
}

you can find a REPO HERE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM