Asynchronous Windows Phone更改照相机类型
我正在尝试在我的应用程序中更改相机的相机类型(前向/主)Asynchronous Windows Phone更改照相机类型,asynchronous,camera,windows-phone,Asynchronous,Camera,Windows Phone,我正在尝试在我的应用程序中更改相机的相机类型(前向/主) <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions>
<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>
因此,我实际上只是在用户单击按钮时更改CameraType。问题是,当用户在按钮上点击几次(比如2秒内点击5次)时,程序无法处理它,并且停止工作。。。如何避免这个问题,有什么解决办法吗
我也尝试过使用en-/禁用该按钮,但我仍然可以单击该按钮
问题是,当用户多次单击按钮时
(比如2秒内5次),程序无法处理它,并且
停止工作
根据我的经验,我注意到,PhotoCamera
类有抛出大量异常的倾向,有时是因为一些模糊的原因
我可能会得到一些反对票,但我会这样做:将代码放入try…catch
块中,如下所示:
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) { }
当然,在使用前置摄像头之前,您需要检查设备是否有:
PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing)
编辑:根据注释,仅使用
try..catch
方法是不够的。
下面是一个更丑陋的解决方案,它应该会起作用:
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;
}
DateTime lastChange=DateTime.MinValue;
已单击私有无效更改(对象发送方,路由目标)
{
TimeSpan elapsedTime=DateTime.Now-lastChange;
if(elapsedTime.totalmillizes<2000)//如果上次更改发生在不到2秒之前,请忽略它
返回;
如果(cam.CameraType==CameraType.FrontFacing)
cam=新的Microsoft.Devices.PhotoCamera(CameraType.Primary);
其他的
cam=新的Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);
viewfinderBrush.Dispatcher.BeginInvoke(委托()
{
viewfinderBrush.SetSource(cam);
});
lastChange=DateTime.Now;
}
我也考虑过试捕,但结果是一样的。多次点击按钮(更改相机类型)将冻结应用程序。没有例外,没有“调试错误”。。。。没有什么。它只是冻结/停止工作(但应用程序仍处于打开状态),Visual Studio也没有给出错误或看到应用程序已冻结,因此我必须通过单击“停止调试”手动停止应用程序当我在新的摄像机类型还没有设置的情况下点击按钮时,就会发生这种情况。哇……在这种情况下,更糟糕的解决方案是检查最后一次更改是否在X秒前进行。是的,这将是一个“丑陋”的解决方案……:/。。。我还尝试在最后{}
使用System.Threading.Thread.Sleep(2500),但是在睡眠之后,相机类型会发生变化?。。。不明白。无论如何,我将尝试使用检查X秒的方法,或者可能只使用主摄像头,如果它不起作用…@Rudi我注意到类似的行为,但仅在附加了调试器的情况下。永远不要在最后的申请。尝试在设备上部署应用程序,手动启动(不使用调试器),然后查看是否可以重现该问题。如果你做不到,请不要费心搜索修复。我编辑了我的答案,告诉你我会怎么做,告诉我它是否有效。
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;
}