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

时间:2017-10-23 10:51:33

标签: c# xamarin nuget-package

我在我的Mac上使用Xamarin。我的计划是制作一个可以扫描二维码并存储信息以供日后使用的应用。我花了很多时间在udemy上学习Xamarin课程和c#课程。我安装了这个包“ZXing.Net.Mobile.Forms”,认为它会让我的生活更轻松。我在代码隐藏中添加了自述文件中的代码,但是它存在一些问题。我没有正确地应用它们吗?

如果有人可以帮助我,或者给我一些提示让我的应用运行起来,我会很高兴。

PS: 似乎该功能要我实际按下按钮进行扫描。应用程序打开后是否可以立即开始扫描,只有在实际找到某些内容或应用程序关闭时才停止扫描?

这是我背后的代码

enter image description here

1 个答案:

答案 0 :(得分:1)

您是否在为应用程序使用XamarinForms?如果是,我认为您无法使用MobileBarcodeScanner方法。你应该使用ZXingScannerPage();

这里是一个样本

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;
    }
}

你可以找到一个REPO HERE