如何在App启动时初始化条形码扫描程序

时间:2017-12-21 12:36:07

标签: xamarin.forms qr-code

我需要我的xamarin跨平台应用程序,只要应用程序启动.QR扫描程序设置为读取代码。在完成扫描时会发出一声嘟嘟声。再次为app准备下一次扫描我怎样才能完成此操作。我所做的是按钮点击扫描仪启动,它的读取代码,然后我必须再次按下按钮再次启动它。

public HomePage()
        {
            Button scanBtn = new Button
            {
                Text = "Scan Barcode",
                HorizontalOptions = LayoutOptions.FillAndExpand,
            };

            scanBtn.Clicked += async (sender, args) =>
            {
                var scanResult = await Acr.BarCodes.BarCodes.Instance.Read();
                if (!scanResult.Success)
                {
                    await this.DisplayAlert("Alert ! ", "Sorry ! \n Failed to read the Barcode !", "OK");
                }
                else
                {
                    var endpoint = new EndpointAddress("http://192.168.15.33/SMS/WebServices/SMSService.svc");
                    var binding = new BasicHttpBinding
                    {
                        Name = "basicHttpBinding",
                        MaxBufferSize = 2147483647,
                        MaxReceivedMessageSize = 2147483647
                    };

                    TimeSpan timeout = new TimeSpan(0, 0, 30);
                    binding.SendTimeout = timeout;
                    binding.OpenTimeout = timeout;
                    binding.ReceiveTimeout = timeout;

                    _client = new SMSServiceClient(binding, endpoint);
                    _client.ValidateStudentAsync("123-admin");
                    _client.ValidateStudentCompleted += _client_ValidateStudentCompleted; ;
                    // await this.DisplayAlert("Scan Successful !", String.Format("Barcode Format : {0} \n Barcode Value : {1}", scanResult.Format, scanResult.Code), "OK");
                }
            };

            Content = new StackLayout
            {
                Children = {
                    scanBtn
                }
            };
        }

和app.cs

 public class App : Application
    {
        public App()
        {
            // The root page of your application
            MainPage = new HomePage();
        }

        protected override void OnStart()
        {
            MainPage = new HomePage();
        }

        protected override void OnSleep()
        {
            MainPage = new HomePage();
        }

        protected override void OnResume()
        {
            MainPage = new HomePage();
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用ZXing.Net.Mobile for Forms来阅读QR码。要初始化此插件,您应该将init方法调用到每个项目(Android,iOS,UWP)中,如下所示:

对于Android中的MainActivity.cs类调用:

ZXing.Net.Mobile.Forms.Droid.Platform.Init();

对于AppDeletage.cs类中的iOS调用

ZXing.Net.Mobile.Forms.iOS.Platform.Init();

最后阅读QR码:

private async void Scan() {
        var scanPage = new ZXingScannerPage();

        scanPage.OnScanResult += (result) => {
            // Stop scanning
            scanPage.IsScanning = false;

            // Pop the page and show the result
            Device.BeginInvokeOnMainThread( async () => {
                await Navigation.PopAsync();
                await DisplayAlert("Scanned Barcode", result.Text, "OK");
            });
        };

        // Navigate to our scanner page
        await Navigation.PushAsync(scanPage);
}