Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Goes Black when using NavigationPage #7

Open
Theoistic opened this issue Nov 30, 2021 · 14 comments · Fixed by osnipezzini/ZXing.Net.Maui#1
Open

Goes Black when using NavigationPage #7

Theoistic opened this issue Nov 30, 2021 · 14 comments · Fixed by osnipezzini/ZXing.Net.Maui#1

Comments

@Theoistic
Copy link

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
Copy link

It works with Android 12/sdk 31+

@hasankhan175
Copy link

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

@SimonLiebers-Dev
Copy link

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
Copy link

danielsvalefelt commented Sep 2, 2022

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
Copy link

nebula2 commented Sep 7, 2022

This is still an issue. The issue lies here:

// The Context here SHOULD be something that's a lifecycle owner
if (Context.Context is AndroidX.Lifecycle.ILifecycleOwner lifecycleOwner)
camera = cameraProvider.BindToLifecycle(lifecycleOwner, cameraSelector, cameraPreview, imageAnalyzer);

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

image

QuickWatch: ContentPage
Snipping Tool: FlyoutPage

@ailiama
Copy link

ailiama commented Nov 22, 2022

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
Copy link

knocte commented Mar 14, 2023

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
Copy link

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

@HaydenFerries
Copy link

HaydenFerries commented Aug 28, 2023

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
Copy link

This is still an issue. The issue lies here:

// The Context here SHOULD be something that's a lifecycle owner
if (Context.Context is AndroidX.Lifecycle.ILifecycleOwner lifecycleOwner)
camera = cameraProvider.BindToLifecycle(lifecycleOwner, cameraSelector, cameraPreview, imageAnalyzer);

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
Copy link

Thank you for the workaround. It worked for me.

@gwalus
Copy link

gwalus commented Apr 16, 2024

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
Copy link

knocte commented Apr 16, 2024

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

@bricefriha
Copy link

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet