ZXing.Net 返回导航 C#

ZXing.Net Back Navigation C#

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

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

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

我发现允许使用 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 */);

我找到了一个可能有用的解决方法。 在内容页面上,创建一个本地内容变量。 如果我实例化扫描仪并将其添加到 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;
    }
}