Make button visible/invisible on ZXing overlay

Viewed 24

I'm using the ZXing plugin to scan bar codes and I'm using a custom overlay to display information and make a button visible/invisible when I need to perform an action, which in this case is to set a flag and make the button invisible again.

In this code I set up the scanning plugin:

        MyButton_Scan.Click += async (sender, e) =>
        {
            var selectedEvent = string.Format("{0}", MyEventsSpinner.GetItemAtPosition(MyEventsSpinner.SelectedItemPosition));
            if (selectedEvent.ToUpper() != "SELECT EVENT")
            {
                MobileBarcodeScanner.Initialize(Application);

                scanner = new MobileBarcodeScanner();

                scanner.UseCustomOverlay = true;

                zxingOverlay = LayoutInflater.FromContext(this).Inflate(Resource.Layout.scanner, null);
                MyScanScreenButton = zxingOverlay.FindViewById<Android.Widget.Button>(Resource.Id.okButton);
                MyScanScreenButton.Click += btnOk_Click;
                MyScanScreenButton.Visibility = Android.Views.ViewStates.Invisible;
                scanner.CustomOverlay = zxingOverlay;

                var opt = new MobileBarcodeScanningOptions();
                opt.DelayBetweenContinuousScans = 5000;
                
                //Start scanning
                scanner.ScanContinuously(this, opt, HandleScanResult);
            } else
            {
                Utils.showMessage("Please select an event from the drop down list");
            }
        };

The code that handles the scan result and button click:

        void btnOk_Click(object sender, System.EventArgs e)
        {
            popUpOpen = false;
            MyScanScreenButton.Visibility = Android.Views.ViewStates.Invisible;
        }

        void HandleScanResult(ZXing.Result result)
        {
            if (!popUpOpen)
            {
                Boolean ConversionGood = true;
                TextView MyTextView = zxingOverlay.FindViewById<TextView>(Resource.Id.ticketInfo);
                Int32 convertedResult = 0;
                Stream successbeepStream = GetType().Assembly.GetManifestResourceStream("eTicket_Scanner.beep.wav");
                Stream failbeepStream = GetType().Assembly.GetManifestResourceStream("eTicket_Scanner.buzzer.wav");
                MyTextView.SetBackgroundColor(Color.White);
                try
                {
                    convertedResult = Convert.ToInt32(result.Text);
                }
                catch (Exception ex)
                {
                    ConversionGood = false;
                    bool isSuccess = _simpleAudioPlayer.Load(failbeepStream);
                    _simpleAudioPlayer.Play();
                    popUpOpen = true;
                    MyScanScreenButton.Visibility = Android.Views.ViewStates.Visible;
                    MyTextView.SetBackgroundColor(Color.Red );
                    MyTextView.Text = "Not a valid ticket for this event.\nErrorif applicable): " + ex.Message;
                }
                string scanResult = "";
                if (ConversionGood)
                {
                    scanResult = MyEventsService.VerifyScannedCode(MyUser.Username, MyUser.Password, currentEventID, convertedResult);

                    if (scanResult == "Y")
                    {
                        MyTextView.SetBackgroundColor(Color.Green);
                        bool isSuccess = _simpleAudioPlayer.Load(successbeepStream);
                        popUpOpen = true;
                        MyTextView.SetBackgroundColor(Color.Green);
                        MyTextView.Text = MyEventsService.GetTicketInfo(MyUser.Username, MyUser.Password, currentEventID, convertedResult);
                        _simpleAudioPlayer.Play();
                        MyScanScreenButton.Visibility = Android.Views.ViewStates.Visible;
                    }
                    else
                    {
                        bool isSuccess = _simpleAudioPlayer.Load(failbeepStream);
                        popUpOpen = true;
                        MyTextView.SetBackgroundColor(Color.Red);
                        MyTextView.Text = MyEventsService.GetFailedScanTicketInfo(MyUser.Username, MyUser.Password, currentEventID, convertedResult);
                        _simpleAudioPlayer.Play();
                        MyScanScreenButton.Visibility = Android.Views.ViewStates.Visible;
                    }
                }
            }
        }

Its not a very complicated app, scan barcodes and verify the code, set the color of the textview background and display the info. The "Ok" button is never visible, but if I click in the space where the button should appear, it executes the button click code. I'm assuming that there is some sort of thread issue here with the interface, but anything I've tried with the thread hasn't worked. Anyone have any ideas?

0 Answers
Related