ZXing.Net.Mobile.Forms页面导航返回时崩溃问题求助(附临时方案)
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:
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; }
- Instead of clearing the entire Content, explicitly stop scanning and release resources when the page disappears:
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.
MVVM Architecture-Specific Adjustments
- FreshMVVM: Leverage the framework's page lifecycle hooks to trigger scanner commands from your ViewModel. For example, add a
StopScanningCommandandStartScanningCommand, then bind them to the page's appearing/disappearing events. - Prism: Implement the
IPageLifecycleAwareinterface in your ViewModel. Use theOnAppearingmethod to start scanning andOnDisappearingto stop it, ensuring resources are managed cleanly within the MVVM pattern.
- FreshMVVM: Leverage the framework's page lifecycle hooks to trigger scanner commands from your ViewModel. For example, add a
Platform-Specific Camera Resource Handling
- On Android, camera resource management is strict. You can create a custom renderer for the
ZXingScannerViewto handle platform-level camera release logic. In the renderer'sOnElementChangedmethod, ensure you release the camera when the element is detached from the view hierarchy.
- On Android, camera resource management is strict. You can create a custom renderer for the
内容的提问来源于stack exchange,提问作者user7885142

