我正在编写一个Windows phone 8应用程序,它具有使用斑马线的条形码扫描模块。
除了下面的角落情况之外它工作正常:当用户足够快以在初始化相机之前将设备置于休眠状态时,等待几秒钟然后唤醒设备,使用CameraOperationCompletedEventArgs触发Initialized事件表示初始化失败。但是,在应用程序重新启动之前,不会再次引发Initialized事件。
可能是什么原因?任何想法如何解决它?
这是一个示例代码。
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>
代码背后:
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
}
}