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
}
}