1

我正在使用 ZXing.Net 相机条形码扫描仪、xamarin 表单和 C#,它似乎运行良好。但是我有一个问题,如果我通过 Navigation.PushAsync() 进入下一页,然后单击返回导航按钮,ZXingScannerPage 相机将不会重新加载......(它只会是最后一张照片的静止图像) ...如何重新加载 ZXingScannerPage 以便相机在按下 Back Navigation 时处于活动状态?无论如何刷新附加到页面的相机视图?

4

3 回答 3

1

使用以下代码。扫描完成后立即停止扫描。不要进行手动操作。

    Entry objScanner= new Entry();
                    objScanner.Placeholder = "Barcode";
                    objScanner.Keyboard = Keyboard.Numeric;
                    objScanner.HorizontalOptions = LayoutOptions.StartAndExpand;
                    objScanner.WidthRequest = Application.Current.MainPage.Width - 40;



                    objScanner.SetBinding(Entry.TextProperty, "ElementValue", BindingMode.TwoWay);
                    objScanner.BindingContext = control;


                    layout.Children.Add(objScanner);

                    objScanner.Focused +=  async (s, e) =>
                    {
                        var scanPage = new ZXingScannerPage();
                        await Navigation.PushAsync(scanPage);

                        scanPage.OnScanResult += (result) =>
                            {
                                // Stop scanning
                                scanPage.IsScanning = false;

                                // Pop the page and show the result
                                Device.BeginInvokeOnMainThread(async () =>
                               {
                                   await Navigation.PopAsync();
                                   objScanner.Text = result.Text;
                                  // await DisplayAlert("Scanned Barcode", result.Text, "OK");
                               });
                            };
                    };
于 2017-04-25T00:03:25.117 回答
0

我找到了一种可能有用的解决方法。在内容页面上,创建一个本地内容变量。如果我实例化扫描仪并将其添加到 OnAppearing 方法中的 Content 中,则设置 Content = null OnDisappearing 方法。清空内容似乎触发了必要的堆栈清理。

这是我的代码:

public class QrCodeScanPage : ZXingScannerPage
{
    View _content;
    public QrCodeScanPage()
    {
        InitScanner();
    }

    void InitScanner()
    {
        IsAnalyzing = true;
        IsScanning = true;
        DefaultOverlayTopText = "Align the barcode within the frame";
        DefaultOverlayBottomText = string.Empty;

        OnScanResult += ScanPage_OnScanResult;

        Title = "Scan Code";

        var item = new ToolbarItem
        {
            Text = "Cancel",
            Command = new Command(async () =>
            {
                IsScanning = false;
                await Navigation.PopAsync();
            })
        };
        if (Device.RuntimePlatform != Device.iOS)
            item.IconImageSource = "toolbar_close.png";

        ToolbarItems.Add(item);
    }

    void ScanPage_OnScanResult(ZXing.Result result)
    {
        Device.BeginInvokeOnMainThread(async () =>
        {
            IsScanning = false;
            IsAnalyzing = false;
            await Navigation.PushAsync(new QrCodeScanResultPage());
        });
    }

    protected override void OnAppearing()
    {
        IsScanning = true;
        IsAnalyzing = true;
        base.OnAppearing();
        if (Content != null)
        {
            _content = Content;
        }
        if (Content == null)
        {
            Content = _content;
        }
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        Content = null;
    }
}
于 2019-09-10T04:15:41.617 回答
0

我发现允许使用 ZXing Scanner 页面进行后退导航的解决方案是在将页面的新实例推送到导航堆栈之前删除 ZXing Scanner 页面的所有实例。在您的 navigation.cs 中,当您准备好推送页面时,使用以下命令:

        foreach(var x in _navigation.Navigation.NavigationStack.ToList())
        {
          if((x.GetType() == typeof(/* name of your scanner page */)))
          {
            _navigation.Navigation.RemovePage(x);
          }
        }
        var page = new /* your scanner page */();
        _navigation.PushAsync( /* your scanner page */);
于 2017-05-04T15:02:27.970 回答