简体   繁体   English

无法正确实现 README 文件中的代码隐藏

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

I'm using Xamarin on my mac.我在我的 Mac 上使用 Xamarin。 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.我花了很多时间在 udemy 上学习 Xamarin 课程和 ac# 课程。 I installed this package "ZXing.Net.Mobile.Forms" thinking it would make my life easier.我安装了这个包“ZXing.Net.Mobile.Forms”,认为它会让我的生活更轻松。 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. Ps:似乎该功能希望我实际按下按钮进行扫描。 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?您是否在为您的应用程序使用 XamarinForms? If Yes I think you can't use MobileBarcodeScanner method.如果是,我认为您不能使用 MobileBarcodeScanner 方法。 You should use ZXingScannerPage();您应该使用 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你可以在这里找到一个回购

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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