'How do I automatically fill my entries with the text generated by scanning a QR code?

So basically something is missing here..

namespace QRFillTest
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            x1.Text = "";
            x2.Text = "Loading time:         " + "asdasdasd";
            x3.Text = "Delivery time:          " + "asdasdasd";
            x4.Text = "Incoterm:                  " + "asdasdasd";
            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
        {
            var scan = new ZXingScannerPage();
            await Navigation.PushModalAsync(scan);

            scan.OnScanResult += (result) =>
            {
                Device.BeginInvokeOnMainThread(async () =>
                {
                    await Navigation.PopModalAsync();
                    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 ......
                    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;
                    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 = "";

                    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;
                        i += 1;



                    }

                    await DisplayAlert("NRSOFTWARE", x1, "OK");
                    await DisplayAlert("NRSOFTWARE", x25, "OK");
                    

                });

            };

        }
    }
}

This code grabs the text from the QR Scanner and cuts it into 26 pieces with unique ID's, so from x1 -> x26, and each ID should go into the corresponding entry hence entry x1 to entry x26.

I don't want a display alert ... i want the QR Code to go to the 26 entries i made. What it does is that it shows me the 26 values I want inside a display alert, with a prompt to click OK at the bottom.



Solution 1:[1]

So I would do this by binding to an ObservableCollection and using a CollectionView or BindableLayout.

In the code behind file you need an ObservableCollection of strings:

public ObservableCollection<string> Words { get; private set; } = new ObservableCollection<string>();

And then in your scan.OnScanResult handler, you go through and split out your words and fill your ObservableCollection:

public void OnScanResult(string result)
{
    Words.Clear();

    var resultsSplit = result.Split(';');

    for (int i = 0; i < resultsSplit.Length; i++)
    {
        switch (i)
        {
            case 1:
                Words.Add($"Loading time: {resultsSplit[i]}");
                break;
            case 11:
                Words.Add($" Bill To: {resultsSplit[i]}");
                break;
            default:
                Words.Add(resultsSplit[i]);
                break;
        }
    }
}

Note the switch case where I'm doing the format strings and actually doing the Words.Add call. I only added a few of the switch cases just as a proof of concept.

Also make sure the BindingContext is set in the constructor:

public MainPage()
{
    InitializeComponent();

    BindingContext = this;
}

The xaml with the BindableLayout is below and is nothing special:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="DeleteMe.MainPage">

    <ScrollView Padding="10">    
        <StackLayout Spacing="10" BindableLayout.ItemsSource="{Binding Words}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Entry Text="{Binding .}" />
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>
</ContentPage>

Running on an iOS sim with the dummy data from your question:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew