Reputation: 936
I'm trying to change the CameraType (FrontFacing/Primary) of my Camera in my app.
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Canvas x:Name="viewfinderCanvas" Width="720" Height="480"
HorizontalAlignment="Left" >
<!--Camera viewfinder -->
<Canvas.Background>
<VideoBrush x:Name="viewfinderBrush" />
</Canvas.Background>
<!-- Brackets for Touch Focus -->
<TextBlock
x:Name="focusBrackets"
Text="[ ]"
FontSize="40"
Visibility="Collapsed"/>
</Canvas>
<!--Button StackPanel to the right of viewfinder>-->
<StackPanel Grid.Column="1" >
<Button Content="Front" Name="btCameraType" Click="changeFacing_Clicked" FontSize="26" FontWeight="ExtraBold" Height="75"/>
</StackPanel>
<!--Used for debugging >-->
<TextBlock Height="40" HorizontalAlignment="Left" Margin="8,428,0,0" Name="txtDebug" VerticalAlignment="Top" Width="626" FontSize="24" FontWeight="ExtraBold" />
</Grid>
And this is the Code Behind:
private void changeFacing_Clicked(object sender, RoutedEventArgs e)
{
if (cam.CameraType == CameraType.FrontFacing)
cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
else
cam = new Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);
viewfinderBrush.Dispatcher.BeginInvoke(delegate()
{
viewfinderBrush.SetSource(cam);
});
}
So I'm actually just changing the CameraType when the user clicks on a button. The problem is that when the user clicks several times on the button (so like 5 times in 2 seconds), the program can't handle it and it stops working... Any solution on how to avoid this problem?
I've also tried en-/disabling the button, but I still can click on the button..
Upvotes: 0
Views: 152
Reputation: 15268
The problem is that when the user clicks several times on the button (so like 5 times in 2 seconds), the program can't handle it and it stops working
In my experience, I've noticed that the PhotoCamera
class has a tendency of throwing a lot of exception, sometimes for obscure reasons.
I might get some downvotes but here is what I would do: put the code in a try...catch
block, like so:
try
{
if (cam.CameraType == CameraType.FrontFacing)
cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
else
cam = new Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);
viewfinderBrush.Dispatcher.BeginInvoke(delegate()
{
viewfinderBrush.SetSource(cam);
});
}
catch (Exception) { }
Of course, before using the FrontFacing camera, you need to check if the device has one:
PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing)
EDIT :
Based on the comments, the try..catch
method is not enough.
Here is an even uglier solution that should work:
DateTime lastChange = DateTime.MinValue;
private void changeFacing_Clicked(object sender, RoutedEventArgs e)
{
TimeSpan elapsedTime = DateTime.Now - lastChange;
if (elapsedTime.TotalMilliseconds < 2000) // If the last change occured less than 2 seconds ago, ignore it
return;
if (cam.CameraType == CameraType.FrontFacing)
cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
else
cam = new Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);
viewfinderBrush.Dispatcher.BeginInvoke(delegate()
{
viewfinderBrush.SetSource(cam);
});
lastChange = DateTime.Now;
}
Upvotes: 2