You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ZXing.Net.Mobile.Forms页面导航返回时崩溃问题求助(附临时方案)

ZXing.Net.Mobile.Forms: RuntimeException: getParameters failed on Navigating Back

I've been working with ZXing.Net.Mobile.Forms in my Xamarin.Forms project, testing both FreshMVVM and Prism architectures. Here's the XAML for my scanner page (ScannerXamlPage.xaml):

<Grid>
    <zxing:ZXingScannerView x:Name="ScannerView" 
                            Result="{Binding ScanResult, Mode=OneWayToSource}" 
                            ScanResultCommand="{Binding ProcessBarcodeCommand}" 
                            IsScanning="{Binding IsScanning}" 
                            IsAnalyzing="{Binding IsAnalyzing}" />
    <zxing:ZXingDefaultOverlay x:Name="ScannerOverlay" 
                               TopText="Hold your phone up to the QR code" 
                               BottomText="Scanning will happen automatically" 
                               ShowFlashButton="True"/>
</Grid>

The issue I'm facing is: when navigating away from this scanner page and then returning to it, the app throws a RuntimeException: getParameters failed exception.

Temporary Workaround

I've found a temporary fix that resolves the issue for now, by clearing the page's content when it disappears:

protected override void OnDisappearing()
{
    base.OnDisappearing();
    Content = null;
}

Root Cause & Permanent Solutions

This exception typically stems from unreleased camera resources—when the page is navigated away from, the ZXing scanner doesn't properly release the camera, so when we return, the system can't reinitialize it. Here are more robust, permanent fixes:

  1. Properly Manage Scanner Lifecycle

    • Instead of clearing the entire Content, explicitly stop scanning and release resources when the page disappears:
      protected override void OnDisappearing()
      {
          base.OnDisappearing();
          ScannerView.IsScanning = false;
          ScannerView.IsAnalyzing = false;
          // If available, call the scanner's dispose method to release camera resources
          ScannerView?.Dispose();
      }
      
      protected override void OnAppearing()
      {
          base.OnAppearing();
          // Re-initialize scanning when the page comes back into view
          ScannerView.IsAnalyzing = true;
          ScannerView.IsScanning = true;
      }
      
  2. Upgrade to the Latest ZXing.Net.Mobile Version

    • This specific issue has been reported in older versions of the library. Check the latest stable release—many resource management bugs have been fixed in updates, so upgrading might resolve the problem outright.
  3. MVVM Architecture-Specific Adjustments

    • FreshMVVM: Leverage the framework's page lifecycle hooks to trigger scanner commands from your ViewModel. For example, add a StopScanningCommand and StartScanningCommand, then bind them to the page's appearing/disappearing events.
    • Prism: Implement the IPageLifecycleAware interface in your ViewModel. Use the OnAppearing method to start scanning and OnDisappearing to stop it, ensuring resources are managed cleanly within the MVVM pattern.
  4. Platform-Specific Camera Resource Handling

    • On Android, camera resource management is strict. You can create a custom renderer for the ZXingScannerView to handle platform-level camera release logic. In the renderer's OnElementChanged method, ensure you release the camera when the element is detached from the view hierarchy.

内容的提问来源于stack exchange,提问作者user7885142

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:46:51