如何使用 C# 和 XAML 将 QR 扫描文本结果输入 Entries/Editors?

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

该项目由两部分组成,第一部分是 xaml 页面设计,其中包含 2 个按钮,第一个按钮用于 QR 扫描仪,第二个按钮用于截取整个表单并将其保存到我的本地图库中,这不是我将使用的实际设计,但我必须使用滚动视图并重新调整设计以使其在我的 phone 上工作并使 QR 扫描按钮可见能够点击它并测试它。

这是我的 Xamarin Forms xaml 页面代码的预览:

<?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>

这是我正在使用的 C# 代码,它不是我的,这就是我遇到问题的原因,我只是在设计,但我收到了这段代码,它应该可以工作。它仅适用于扫描二维码并显示其信息,我知道这是因为 DisplayAlert 但我遇到的唯一问题是代码没有将扫描的文本放入相应的条目中。

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");
        }

    }
}

我看过你的代码,发现你只是在构造方法中设置了一次Entry.Text。当您单击按钮并获得扫描仪结果时,您没有更新条目和标签的文本值。

所以你可以尝试更改foreach中的代码,如:

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

          i += 1;
        }