3

I'm writing a Windows phone 8 application that has a barcode scanning module that uses Zebra crossing.

It works fine except for the following corner case: when the user is fast enough to put the device to sleep before the camera is initialized, waits for a few seconds and then wakes the device up, the Initialized event is fired with CameraOperationCompletedEventArgs that indicates unsuccessful initialization. That is to be expected, however, the Initialized event is not raised again until the application is restarted.

What could be the reason? Any ideas how to fix it?

Here's an example code.

XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Canvas x:Name="viewfinderCanvas" Margin="20, 0, 20, 0">
            <!--  Camera viewfinder  -->
            <Canvas.Background>
                <VideoBrush x:Name="viewfinderBrush">
                    <VideoBrush.RelativeTransform>
                        <CompositeTransform x:Name="viewfinderTransform"
                                            CenterX="0.5"
                                            CenterY="0.5"
                                            Rotation="90" />
                    </VideoBrush.RelativeTransform>
                </VideoBrush>
            </Canvas.Background>
        </Canvas>           
    </Grid>

Code behind:

    PhotoCamera camera;
    // constructor
    public ScanPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        camera = new PhotoCamera();
        camera.Initialized += camera_Initialized;
        try
        {
            viewfinderBrush.SetSource(camera);
        }
        catch (Exception) { }

        base.OnNavigatedTo(e);
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        // clean up resources
        try
        {
            camera.Initialized -= camera_Initialized;
            camera.Dispose();
        }
        catch (Exception ex)
        {
            // handling
        }

        base.OnNavigatedFrom(e);
    }

    private void camera_Initialized(object sender, CameraOperationCompletedEventArgs e)
    {
        if (e.Succeeded)
        {
            // elided, use camera                
        }            
    }
user2936023
  • 131
  • 6
  • I'm no pro, but is OnNavigatedTo firing when the app is started again after putting it to sleep? – user3007447 Aug 30 '14 at 08:13
  • @user3007447 That is right. OnNavigatedTo is called when navigating to a new instance of a page, as well as when we are resuming the application and navigating to an already existing instance. – user2936023 Sep 01 '14 at 06:51

0 Answers0