0

我正在查看Xamarin.Forms 的 ZXing.net示例,它使用仅代码方法来扫描条形码。

我以这个问题为灵感来获取以下代码,但我不完全确定如何在使用 Prism 时管理扫描事件。

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:zx="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
             xmlns:zxcm="clr-namespace:ZXing.Common;assembly=zxing.portable"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="WebOfTrust.Views.Client.MyPeople.ScanNewOrUpdateContact">
    <ContentPage.Content>
        <StackLayout>


            <zx:ZXingDefaultOverlay
                TopText= " Hold your phone up to the barcode"
                BottomText=" Scanning will being automatically">
            </zx:ZXingDefaultOverlay>


            <!--     BarcodeValue="{Binding QrCode}"    -->
            <zx:ZXingScannerView
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand"> 
            </zx:ZXingScannerView>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我的问题是,我如何设计一个页面,以便我可以捕获 OnScanResult,如示例中所示(复制如下)

        zxing.OnScanResult += (result) => 
            Device.BeginInvokeOnMainThread (async () => {

                // Stop analysis until we navigate away so we don't keep reading barcodes
                zxing.IsAnalyzing = false;

                // Show an alert
                await DisplayAlert ("Scanned Barcode", result.Text, "OK");

                // Navigate away
                await Navigation.PopAsync ();
            });
4

1 回答 1

2

参考以下代码:

在xml中

<zx:ZXingScannerView
    x:Name="zxing"
    IsAnalyzing="{Binding IsAnalyzing,Mode=TwoWay}" 
    Result="{Binding Result, Mode=TwoWay}"
    ScanResultCommand="{Binding ScanResultCommand}"                
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand"> 
</zx:ZXingScannerView> 

在视图模型中

public ZXing.Result Result { get; set; }

public bool IsAnalyzing { get;set }

public Command ScanResultCommand
{
   get
    {
       return new Command(() =>
        {

          Device.BeginInvokeOnMainThread(async () =>
            {
              IsAnalyzing = false;

              Console.WriteLine(Result.Text);

              //...

             });
            });
     }
}
于 2018-10-03T09:21:36.550 回答