繁体   English   中英

Xamarin Forms 代码在绑定和访问 xaml 控件后

[英]Xamarin Forms code behind binding and accessing xaml controls

我有一个希望相当简单的问题。 我正在创建一个简单的 xamarin forms 应用程序,该应用程序使用 zxing 扫描条形码,目前我只希望它在表单的输入字段中显示它,但绑定不起作用,注释代码也不能设置值后面的代码。 事实上,出于某种原因,在这个简单的项目中,直接从后面的代码访问表单项只会在调试时冻结应用程序。 希望有人可以指出导致此问题的代码中的一些简单问题。 谢谢。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms" 
         x:Class="Inventory.MainPage">
<ContentPage.Content>
    <StackLayout>
        <Grid x:Name="gridScanner">
            <zxing:ZXingScannerView x:Name="zxing" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" OnScanResult="ZXingScannerView_OnScanResult" />
            <zxing:ZXingDefaultOverlay x:Name="overlay" TopText="" BottomText="Barcode will be scanned automatically" ShowFlashButton="False" />
        </Grid>
        <StackLayout>
            <Label x:Name="lblSku" Text="SKU" FontAttributes="Bold" FontSize="Title"/>
            <Entry x:Name="txtSku" Placeholder="SKU" ReturnType="Next" />
        </StackLayout>
    </StackLayout>
</ContentPage.Content>
    public MainPage()
    {
        

        InitializeComponent();
        _currentItem = new ScanItem();
        txtSku.BindingContext = _currentItem.SKU;
        txtSku.SetBinding(Entry.TextProperty, "Value");

        zxing.IsScanning = true;
    }

    public void ZXingScannerView_OnScanResult(Result result)
    {
        _currentItem.SKU = result.Text;
        //txtSku.Text = result.Text;

    }

在不使用 ScannerView 的情况下,我已经完成了您想要的完全相同的事情。 我的页面中有一个 CollectionView、一个条目和一个按钮。 CollectionView 在这里无关紧要,所以我将跳过它。 我的按钮会打开一个扫描仪。

<Button Text="QR Scan"
    Grid.Column="1"
    Grid.Row="1"
    TextColor="White"
    CornerRadius="30"
    Clicked="ButtonScan" />

我的条目是:

<Entry Grid.ColumnSpan="2"
    BackgroundColor="White"
    IsTextPredictionEnabled="False"
    TextTransform="Uppercase"
    FontSize="Body"
    TextChanged="Search"
    Placeholder="Αναζήτηση"
    TextColor="Black"
    PlaceholderColor="Black"
    x:Name="lblBarcode"
    Keyboard="Chat"/>

点击事件:

private async void ButtonScan(object sender, EventArgs e)
    {
        try
        {
            IQrScanningService scanner = DependencyService.Get<IQrScanningService>();
            string result = await scanner.ScanAsync();
            if (result == "noPermission")
            {
                await DisplayAlert("Permissions", "No permissions to access camera.", "ΟΚ");
                //Environment.Exit(1);
            }
            if (result != null && result != "")
            {
                lblBarcode.Text = result; // <--- This works for me.
                //collectionView.FilterString = "Contains([VoucherString], " + result + ")";
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

我正在使用服务打开扫描仪。 它也可以通过其他方式完成,但我选择了这个。

public class QrScanningService : IQrScanningService
{
    public async Task<string> ScanAsync()
    {
        if (ContextCompat.CheckSelfPermission(Android.App.Application.Context, Manifest.Permission.Camera) != (int)Permission.Granted)
        {
            return "noPermission";
        }
        MobileBarcodeScanningOptions optionsCustom = new MobileBarcodeScanningOptions();

        MobileBarcodeScanner scanner = new MobileBarcodeScanner()
        {
            UseCustomOverlay = false,
            TopText = "Scan the Barcode",
            BottomText = "Align the red line with the barcode"
        };

        ZXing.Result scanResult = await scanner.Scan(optionsCustom);
        return scanResult?.Text;
    }
}

编辑

更简单的方法是不使用服务。

MobileBarcodeScanner scanner1 = new MobileBarcodeScanner();
var result1 = await scanner1.Scan();
lblBarcode.Text = result1.Text;
scanner1.Cancel();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM