TextChanged event triggers multiple times

367 Views Asked by At

I am working on android app for Honeywell EDA51 (barcode scanner). On certain pages in entry field I want to make it work for both scanning with EDA51 and manual typing (character by character)

When I tried using Completed() event, I get good functionality for manual typing but while scanning it has problem because it always request from user to manually complete input (press )

Then I implemented it using TextChanged() event where I ignore (just concatenate) input until it reach certain length.

private void ProductBarcodeEntry_TextChanged(object sender, TextChangedEventArgs e)
    {
        var entry = (Entry)sender;
        entry.TextChanged -= ProductBarcodeEntry_TextChanged;
        try
        {
            var context = (DescriptionViewModel)this.BindingContext;
            if (context.controlValue%4 == 0)
            {
                context.controlValue = 1;
                context.Barcode = context.BarcodeProduct;
                bool p = context.ExecuteScanCommand().Result;
                if (!p)
                {
                    this.ProductBarcodeEntry.Text = context.Barcode;
                    this.ProductBarcodeEntry.CursorPosition = context.Barcode.Length;
                }
                else 
                {
                    this.BoxBarcodeEntry.Text = context.BoxID;
                    this.ProductBarcodeEntry.Text = "";
                    this.ProductBarcodeEntry.Focus();
                }
            }
            else
            {
            }
            context.controlValue++;
        }

        catch (Exception ex)
        {
            
        }
        finally
        {
            entry.TextChanged += ProductBarcodeEntry_TextChanged; 
        }

Those control values are something I tried to catch how much times does it triggers event. At first I thought it fires when ever in this code I manipulate with this.ProductBarcodeEntry.Text but when I follow execution in debugger it doesn't recall event when it reach that point, it goes normal to the final segment and after that it starts again. I got about 4 calls but to be honest at some point both device and debugger just doesn't respond, somewhere inside code when it is stopped on some breakpoint after step over it just ignore rest of breakpoints, and device after some times do what it was supposed.

private void BoxBarcodeEntry_TextChanged(object sender, TextChangedEventArgs e)
    {
        var entry = (Entry)sender;
        entry.TextChanged -= BoxBarcodeEntry_TextChanged;
        try
        {
            var context = (DescriptionViewModel)this.BindingContext;
            if (context.controlValue == 0)
            {
                context.controlValue = 1;
                context.Barcode = context.BarcodeBox;
                bool p = context.ExecuteScanCommand().Result;
                //bool p = context.ScanCommand.Execute(this);
                if (!p)
                {
                    this.BoxBarcodeEntry.Text = context.Barcode;
                    this.BoxBarcodeEntry.CursorPosition = context.Barcode.Length;
                }
                else 
                {
                    this.BoxBarcodeEntry.Text = context.BoxID;
                    this.ProductBarcodeEntry.Text = "";
                    this.ProductBarcodeEntry.Focus();
                }
            }
            else
            {
            }
        }

        catch (Exception ex)
        {
            
        }
        finally
        {
            entry.TextChanged += BoxBarcodeEntry_TextChanged;
            

        }
    }

And XAML part

    <Entry Grid.Row="2" TextChanged="BoxBarcodeEntry_TextChanged" HorizontalOptions="CenterAndExpand" x:Name="BoxBarcodeEntry" Placeholder="Scan box barcode" Text="{Binding input.BarcodeBox, Mode=TwoWay}" TextColor="#2A5B90" WidthRequest="300"/>
    <Entry Grid.Row="1" TextChanged="ProductBarcodeEntry_TextChanged" HorizontalOptions="CenterAndExpand" x:Name="ProductBarcodeEntry" Placeholder="Scan product barcode" Text="{Binding input.BarcodeProduct, Mode=TwoWay}" TextColor="#2A5B90" WidthRequest="300"/>

BoxBarcodeEntry_TextChanged() is not done, its not frequently used as product and it was useless to do same thing when product scan doesn't work well (Just for reference it never goes to this method even when BoxBarcodeEntry.Text is changed from ProductBarcodeEntry_TextChanged()).

Functionality that I want to achieve is to have two entry fields on page, focus will be on product box because some of product doesn't have box attached to it, but anyway when user scan barcode it validates (if description is about product with box, then if it is box code it stores value in BoxBarcodeEntry, and save it in memory, then after that until next BoxBarcode is scanned it makes pairs (Box,Products) etc).

Description of products with box example: (shorts bb - BoxBarcode, pb - ProductBarcode )

Scans:

  • pb1 - ignores it (bb in memory is null), and clear text field
  • bb1 - store in memory and fills BoxBarcodeEntry.Text
  • bb2 - store (replace old) in memory and fills BoxBarcodeEntry.Text
  • pb2 - pairs with bb from memory -> (bb2,pb2) is stored somewhere in DB
  • pb3 - pairs with bb from memory -> (bb2,pb3) is stored somewhere in DB
  • ...

And example of description for products without box

  • pb1 - (null,pb1) is stored in DB
  • bb1 - ignored
  • bb2 - ignored
  • pb2 - (null,pb2) is stored in DB
  • pb3 - (null,pb3) is stored in DB
  • ...

Maybe I wrote too much information but main question remains same, why does it triggers those events multiple times and how to prevent (or control) that behavior.

0

There are 0 best solutions below