如何在Xamarin表单中与其他UI元素一起使用ZXing.Net扫描仪?

时间:2018-10-02 22:05:21

标签: c# xamarin.forms prism zxing.net

我正在查看ZXing.net for Xamarin.Forms的样本,它使用的是仅代码扫描条形码的方法。

我使用this question as inspiration来获取以下代码,但是 我不确定使用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 ();
            });

1 个答案:

答案 0 :(得分:0)

请参考以下代码:

  

在xaml中

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

在ViewModel中

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

              //...

             });
            });
     }
}