如何将Barcodereader-Method的结果导入我的构造函数(使其可访问)?

时间:2017-09-11 14:43:45

标签: c# xamarin.forms

如何访问我的Scan() Clicked Event上调用的方法Button读取的结果字符串?

我只想在后台传递数据并从一个点访问它们,将它们组合成一个组合字符串,用于界面目的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TDE
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        public Page1 ()
        {
            InitializeComponent ();

            var layout = new StackLayout()
            {
                Margin = 10,
                Spacing = 20
            };
            var label = new Label
            {
                Text = "Bitte Scannen Sie den Barcode und geben noch folgende Informationen ein, um den Vorgang abzuschließen:",
                FontSize = 30,
                FontAttributes = FontAttributes.Bold
            };
            var button = new Button
            {
                Text = "Hier Fahrzeug Scannen",
                TextColor = Color.FromHex("#004d93"),
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            var entry_Kraftstoffmenge = new Entry
            {
                Placeholder = "Getankte Kraftstoffmenge in Liter",
                PlaceholderColor = Color.FromHex("#004d93"),
                HorizontalTextAlignment = TextAlignment.Center
        };
            var entry_Fahrer = new Entry
            {
                Placeholder = "Hier Ihr Name / Kürze",
                PlaceholderColor = Color.FromHex("#004d93"),
                HorizontalTextAlignment = TextAlignment.Center
            };
            var entry_KMStd = new Entry
            {
                Placeholder = "KM-Stand in KM / Stunden in h",
                PlaceholderColor = Color.FromHex("#004d93"),
                HorizontalTextAlignment = TextAlignment.Center
            };
            var entry_Geraet = new Entry
            {
                Placeholder = "Gerätename",
                PlaceholderColor = Color.FromHex("#004d93"),
                HorizontalTextAlignment = TextAlignment.Center
            };

            layout.Children.Add(label);
            layout.Children.Add(button);
            layout.Children.Add(entry_Kraftstoffmenge);
            layout.Children.Add(entry_Fahrer);
            layout.Children.Add(entry_KMStd);
            layout.Children.Add(entry_Geraet);

            Content = layout;

            button.Clicked += ButtonScannen;

        }

        void ButtonScannen(object sender, EventArgs e)
        {
            Scan();
        }

        public async void Scan()
        {
            var scanner = new ZXing.Mobile.MobileBarcodeScanner();
            var result = await scanner.Scan();
            var _tankbomb = "";
            if (result != null)
            {
                _tankbomb = result.ToString();
                await DisplayAlert("Erkanntes Fahrzeug", _tankbomb, "Ok");
            }
            else
            {
                _tankbomb = "000000";
                await DisplayAlert("Fahrzeug nicht erkannt", "Es wurde ein Dummy-Fahrzeug eingetragen!", "Ok");
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

最简单的方法是将值作为参数传递给Page2的构造函数。

public async void Scan()
{
   var scanner = new ZXing.Mobile.MobileBarcodeScanner();
   var result = await scanner.Scan();
   var _tankbomb = "";
   if (result != null)
   {
       _tankbomb = result.ToString();
       await Navigation.PushAsync(new Page2(_tankbomb));
   }
   else
   {
       _tankbomb = "000000";
       await DisplayAlert("Fahrzeug nicht erkannt", "Es wurde ein Dummy-Fahrzeug eingetragen!", "Ok");
   }
}

此外,您必须在Page2构造函数中添加一个属性,如下所示:

public Page2 (string data)
{
   string readData = data;
   Label lbl_name = new Label {
       Text ="hello "+ readData
   };
   Title="Second Page";
}
  

另一个选择是你可以在Page2上创建一个公共属性,并在创建它之后为其赋值。