Redth / ZXing.Net.Maui

Barcode Scanning for MAUI?
MIT License
467 stars 152 forks source link

Goes Black when using NavigationPage #7

Open Theoistic opened 2 years ago

Theoistic commented 2 years ago

Only way atm to use the components is by setting the MainPage manually, when pushing and poping using navigation it will cause the component not to initialize.

sanfordmj commented 2 years ago

It works with Android 12/sdk 31+

hasankhan175 commented 2 years ago

It does not work even with Android 12/sdk 31+ when it is used inside navigation page. Is there a workaround

SimonLiebers-Dev commented 2 years ago

Has someone found a solution or workaround? It is still not working for me. Works fine when i set the MainPage. But if i use it with NavigationPage, i only see a black screen.

danielsvalefelt commented 2 years ago

I dot the same problem using AppShell and tabbed pages. When tabbing to another page and going back, the camera disappears. The camera object is probably destroyed when changing to another tab, and not reinitialized when the page gets in focus.

Could be that the MainPage get initialized before it get focused, and when it get focused it is never properly initialized.

It was easy to reproduce in the BigIslandBarcode project

Replace MainPage = new MainPage(); with MainPage = new NavigationPage(new MainPage());

nebula2 commented 2 years ago

This is still an issue. The issue lies here:

https://github.com/Redth/ZXing.Net.Maui/blob/71a6ea2114b9f1412dccedee36ad1721e06e5a4a/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs#L100-L102

When using a FlyoutPage (for example), Context.Context is not a Lifecycle owner.

image

QuickWatch: ContentPage Snipping Tool: FlyoutPage

ailiama commented 1 year ago

Hello,

I am try ZXing.Net.Maui on .NET 6 Framework and sometimes the camera just turns black. Is anyone know what is the fix for this issue?

I have also noticed an other preview version of ZXing.Net.Maui on NuGet supporting NET 7, do you know if the black screen issue will be addressed on that version ?

Thank you

knocte commented 1 year ago

When using a FlyoutPage (for example), Context.Context is not a Lifecycle owner.

If you understand the bug, don't you know how to fix it upstream? (i.e. propose a PR?)

nullDorian commented 1 year ago

Same issue here, with a NavigationPage the view is Black. .NET 6, Android 12

HaydenFerries commented 1 year ago

I found a workaround for this, you need to recreate and re-assign the camera view with something like the following:

Add to Page Init: Appearing += Page_Appearing;
    private void Page_Appearing(object sender, EventArgs e)
    {
        RecreateCameraView();
    }
    private void RecreateCameraView()
    {
        // Create a new instance for CameraBarcodeReaderView
        var newCameraView = new CameraBarcodeReaderView()
        {
            Options = new BarcodeReaderOptions
            {
                AutoRotate = true,
                Multiple = false,
                TryHarder = true,
                TryInverted = true,
                Formats = BarcodeFormat.QrCode
            },
            CameraLocation = CameraLocation.Rear
        };

        newCameraView.BarcodesDetected += cameraView_BarCodeDetected;

        var index = CameraGrid.Children.IndexOf(CameraView);
        CameraGrid.Children.Remove(CameraView);
        `CameraGrid.Children.Insert(index,newCameraView);`

        // Assign your new instance to CameraView.
        CameraView = newCameraView;
    }

Replace "CameraView" with the name of your CameraBarcodeReaderView, Replace "CameraGrid" with the name of the Parent container. Adjust the Options and listeners etc to whatever you are needing.

mikebikerider commented 12 months ago

This is still an issue. The issue lies here:

https://github.com/Redth/ZXing.Net.Maui/blob/71a6ea2114b9f1412dccedee36ad1721e06e5a4a/ZXing.Net.MAUI/Platforms/Android/CameraManager.android.cs#L100-L102

When using a FlyoutPage (for example), Context.Context is not a Lifecycle owner.

image

QuickWatch: ContentPage Snipping Tool: FlyoutPage

I found a workaround for this, you need to recreate and re-assign the camera view with something like the following:

Add to Page Init: Appearing += Page_Appearing;
    private void Page_Appearing(object sender, EventArgs e)
    {
        RecreateCameraView();
    }
    private void RecreateCameraView()
    {
        // Create a new instance for CameraBarcodeReaderView
        var newCameraView = new CameraBarcodeReaderView()
        {
            Options = new BarcodeReaderOptions
            {
                AutoRotate = true,
                Multiple = false,
                TryHarder = true,
                TryInverted = true,
                Formats = BarcodeFormat.QrCode
            },
            CameraLocation = CameraLocation.Rear
        };

        newCameraView.BarcodesDetected += cameraView_BarCodeDetected;

        var index = CameraGrid.Children.IndexOf(CameraView);
        CameraGrid.Children.Remove(CameraView);
        `CameraGrid.Children.Insert(index,newCameraView);`

        // Assign your new instance to CameraView.
        CameraView = newCameraView;
    }

Replace "CameraView" with the name of your CameraBarcodeReaderView, Replace "CameraGrid" with the name of the Parent container. Adjust the Options and listeners etc to whatever you are needing.

mikebikerider commented 12 months ago

Thank you for the workaround. It worked for me.

gwalus commented 6 months ago

Solution it's working for me is open CameraBarcodeReaderView in Shell modal

Viewmodel:

await Shell.Current.Navigation.PushModalAsync(new CameraPage(), true);

Xaml Page (modal):

<zxing:CameraBarcodeReaderView Options="{Binding BarcodeOptions}">
    <zxing:CameraBarcodeReaderView.Behaviors>
        <toolkit:EventToCommandBehavior
            EventName="BarcodesDetected"
            Command="{Binding BarcodeDetectedCommand}"
            EventArgsConverter="{StaticResource BarcodeDetectedEventArgsConverter}"/>
    </zxing:CameraBarcodeReaderView.Behaviors>
</zxing:CameraBarcodeReaderView>

note: This is a solution for MVVM pattern.

knocte commented 6 months ago

Can someone propose a PR instead of posting workarounds? :)

bricefriha commented 6 months ago

Can someone propose a PR instead of posting workarounds? :)

I agree, I can take a look at this this weekend if you all want