Home > Net >  How can I input the QR scan text result into Entries/Editors using C# and XAML?
How can I input the QR scan text result into Entries/Editors using C# and XAML?

Time:05-28

The Project is made up of 2 parts, the first part is the xaml page design which includes 2 buttons, the first button is for the QR Scanner and the second button takes a screenshot of the whole form and saves it into my native gallery, this is not the actual design I will use but I had to use scrollview and re-adjust the design in order to make it work on my phone and make the QR Scan Button visible to be able to click it and test it.

Here is a preview of my Xamarin Forms xaml page code:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:forms="clr-namespace:SignaturePad.Forms;assembly=SignaturePad.Forms"
    xmlns:local="clr-namespace:SignatureSample"
    xmlns:effects="clr-namespace:PlainEntryAndroidSample.Effects" xmlns:effects1="clr-namespace:QRTest3.Effects"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="QRTest3.NewPage">
    <ContentPage.Content >
        <ScrollView>
        <StackLayout BackgroundColor="White" Padding="10">
            <StackLayout>
                <Label Text="Tel: 00 961 44434445/6/7"
                       FontSize="12"
                       TextColor="Black"/>
                <Label x:Name="x2" />
                <Label x:Name="x3" />
                <Label x:Name="x4" />
                <Label Text="Fax: 00 961 4437 2041"
                       FontSize="12"
                       TextColor="Black"/>
                <Label Text="P.O. Box 4694"
                       FontSize="12"
                       TextColor="Black"/>
            </StackLayout>

                <Label Text="Delivery Note"
                       FontSize="19"
                       TextColor="Black"
                       FontAttributes="Bold" />
                <Image x:Name="logo1" Source="companylogo" HorizontalOptions="Start" Margin="550,-100,0,0" HeightRequest="108" WidthRequest="115"/>
            <StackLayout>
                            <Label Text="Truck plate number: "
                                   TextColor="Black"
                                   x:Name="x5"
                                   />
                            <Label Text="Contract number: "
                                   TextColor="Black"
                                   x:Name="x6"
                                   />
                            <Label Text="Driver name: "
                                   TextColor="Black"
                                   x:Name="x7"
                                   />
                            <Button Text="Scan your QR code &#x000f0432; "
                                    FontSize="12.5"
                                    Margin="10,30,10,30"
                                    FontFamily="MYICON"
                                    Clicked="Button_Clicked"
                                    CornerRadius="0"
                                    BorderColor="Black"
                                    BorderWidth="1"
                                    Padding="0,0,0,0"
                                    HorizontalOptions="Start"
                                    BackgroundColor="#80BFE4"
                                    WidthRequest="130"
                                    HeightRequest="50"
                                    TextColor="Black"
                                    FontAttributes="None" />
                            <Entry  x:Name="x1" Margin="300,-30,200,0" FontSize="15" HorizontalTextAlignment="Center" Placeholder="">
                                    <Entry.Effects>
                                        <effects1:PlainEntry/>
                                    </Entry.Effects>
                                </Entry>
            </StackLayout>
                <Entry x:Name="x8" />
                <Entry x:Name="x9" />
                <Entry x:Name="x10" />
                <Entry x:Name="x11" />
                <Entry x:Name="x12" />
                <Entry x:Name="x13" />
                <Entry x:Name="x14" />
                <Entry x:Name="x15" />
                <Entry x:Name="x16" />
                <Entry x:Name="x17" />
                <Entry x:Name="x18" />
                <Entry x:Name="x19" />
                <Entry x:Name="x20" />
                <Entry x:Name="x21" />
                <Entry x:Name="x22" />
                <Entry x:Name="x23" />
                <Entry x:Name="x24" />
                <Entry x:Name="x25" />
                <Entry x:Name="x26" />
                <Entry x:Name="x27" />
                <Button Text="Document filled"                          
                        FontSize="12.5"
                        Margin="0"
                        HorizontalOptions="Center"
                        FontFamily="MYICON"
                        BorderColor="Black"
                        Padding="0"
                        BorderWidth="1"
                        BackgroundColor="White"
                        WidthRequest="120"
                        HeightRequest="23"
                        CornerRadius="0"
                        Clicked="DoPDFThings"
                        TextColor="Black"
                        FontAttributes="Bold"/>
                </StackLayout>
            </ScrollView>
    </ContentPage.Content>
</ContentPage>

And here's the C# code I'm using, it's not mine which is why I'm having issues, I'm only working the design but I received this code and it was expected to work. it only works in terms of scanning QR Codes and displaying their information and I understand this is because of the DisplayAlert but the only issue I'm having is the code not putting the scanned text into respective entries.

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using ZXing.Net.Mobile.Forms;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;
using System.IO;
using System.Drawing;
using System.Collections.ObjectModel;
using Plugin.XamarinFormsSaveOpenPDFPackage;
using System.Net.Http;
using Xamarin.Essentials;
using NativeMedia;
using ImageFromXamarinUI;

namespace QRTest3
{
    public partial class NewPage : ContentPage
    {

        public NewPage()
        {

            InitializeComponent();

            x1.Text = "";
            x2.Text = "Loading time:         "   "";
            x3.Text = "Delivery time:          "   "";
            x4.Text = "Incoterm:                  "   "";
            x5.Text = "Truck plate number: "   "";
            x6.Text = "Contract number: "   "";
            x7.Text = "Driver name: "   "";
            x8.Text = "Ship To:                                                                  "   "";
            x9.Text = ""; 
            x10.Text = ""; 
            x11.Text = ""; 
            x12.Text = "Bill To:                                                                 "   ""; 
            x13.Text = ""; 
            x14.Text = ""; 
            x15.Text = ""; 
            x16.Text = "Sold To                                                                  "   ""; 
            x17.Text = ""; 
            x18.Text = ""; 
            x19.Text = ""; 
            x20.Text = ""   ""; 
            x21.Text = ""; 
            x22.Text = ""; 
            x23.Text = ""; 
            x24.Text = ""   "";
            x25.Text = "";
            x26.Text = "Remark: "   "";
        }


        async void Button_Clicked(System.Object sender, System.EventArgs e) // was private (Scan button)
        {
            var scan = new ZXingScannerPage();
            await Navigation.PushModalAsync(scan); //PushModalAsync presents a page modally, with optional animation. (presents the scanner cam)


            scan.OnScanResult  = (result) =>
            {

                Device.BeginInvokeOnMainThread(async () => //An async method runs synchronously until it reaches its first await expression, at which point the method is suspended until the awaited task is complete.
                { //BeginInvokeOnMainThread checks if the current thread is the main thread, and if so, it invokes the action immediately

                    await Navigation.PopModalAsync(); //Asynchronously dismisses the most recent modally presented Page, with optional animation. (removes scanner after scanning qr code)
                    await DisplayAlert("Valeur QRCODE", ""   result.Text, "OK");
                    //txtinput.Text = result.Text;
                    


                    string xd;
                  /*  // xd the QR code ........................
                    xd = "301232189;"   //x1
                       "5/12/2022 08:05:32;"   //x2
                       "5/12/2022 08:05:32;"   //x3
                       "EXW;"   //x4
                       "161410;"   //x5
                       "000123892;"   //x6
                       "ISAKA KISKOUSJ;"   //x7
                       "0112398901;"   //x8
                       "ISMET READY MIX;"  //x9
                       "STREET TANOUS A L ACHKAR;"   //x10
                       "DOHA QA;"  //x11
                       "0112398901;"  //x12
                       "ISMET READY MIX;"  //x13
                       "STREET TANOUS A L ACHKAR;"  //x14
                       "DOHA QA;"  //x15
                       "0112398901;"  //x16
                       "ISMET READY MIX;"  //x17
                       "STREET TANOUS A L ACHKAR;"  //x18
                       "DOHA QA;"  //x19
                       "14000256;"  //x20
                       "GBFS BULK;"  //x21
                       "16.28;"  //x22
                       "41.17;"  //x23
                       "30.18;"  //x24
                       "t;"  //x25
                       "10"; //x26   // read by QR CODE ...... */
                    xd = result.Text;

                    string[] words = xd.Split(';');



                    string x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27;
                    x1 = "";
                    x2 = "";
                    x3 = "";
                    x4 = "";
                    x5 = "";
                    x6 = "";
                    x7 = "";
                    x8 = "";
                    x9 = "";
                    x10 = "";
                    x11 = "";
                    x12 = "";
                    x13 = "";
                    x14 = "";
                    x15 = "";
                    x16 = "";
                    x17 = "";
                    x18 = "";
                    x19 = "";
                    x20 = "";
                    x21 = "";
                    x22 = "";
                    x23 = "";
                    x24 = "";
                    x25 = "";
                    x26 = "";
                    x27 = "";


                    int i = 1;
                    foreach (var word in words)
                    {
                        //  await DisplayAlert("NRSOFTWARE", word , "OK"); ;
                        if (i == 1) x1 = word;
                        if (i == 2) x2 = word;
                        if (i == 3) x3 = word;
                        if (i == 4) x4 = word;
                        if (i == 5) x5 = word;
                        if (i == 6) x6 = word;
                        if (i == 7) x7 = word;
                        if (i == 8) x8 = word;
                        if (i == 9) x9 = word;
                        if (i == 10) x10 = word;
                        if (i == 11) x11 = word;
                        if (i == 12) x12 = word;
                        if (i == 13) x13 = word;
                        if (i == 14) x14 = word;
                        if (i == 15) x15 = word;
                        if (i == 16) x16 = word;
                        if (i == 17) x17 = word;
                        if (i == 18) x18 = word;
                        if (i == 19) x19 = word;
                        if (i == 20) x20 = word;
                        if (i == 21) x21 = word;
                        if (i == 22) x22 = word;
                        if (i == 23) x23 = word;
                        if (i == 24) x24 = word;
                        if (i == 25) x25 = word;
                        if (i == 26) x26 = word;
                        if (i == 27) x27 = word;

                        i  = 1;
                    }

                    xd = result.Text;
                    await DisplayAlert("Valeur QRCODE", ""   x1, "OK");
                    
                });

            }; 

        }


        private async void DoPDFThings(object sender, EventArgs e)
        {
            var screenshot = await Screenshot.CaptureAsync(); //    working 100% for partial sc: was var imageStream = await header.CaptureImageAsync();

            //  working 100% for partial sc: await MediaGallery.SaveAsync(MediaFileType.Image, imageStream, "myScreenshot.png");

            //  var screenshot = await Screenshot.CaptureAsync();

            await MediaGallery.SaveAsync(MediaFileType.Image, await screenshot.OpenReadAsync(), "myScreenshot.png");
        }

    }
}

CodePudding user response:

I had read your code and found that you just set the Entry.Text in the construction method once. When you click the button and get the scanner result, you didn't update the Text value of the Entry and the Label.

So you can try to change the code in the foreach, such as:

foreach (var word in words)
      {
         //  await DisplayAlert("NRSOFTWARE", word , "OK"); ;
          if (i == 1) x1.Text = word;
          if (i == 2) x2.Text = word;
          ......

          i  = 1;
        }
  • Related