在我的应用程序中,您可以打开一个可以打开和关闭手电筒的站点。 它第一次工作,但如果我尝试第二次切换手电筒,应用程序崩溃。
我认为这是AudioVideoCaptureDevice.OpenAsync的一个问题。如果我第二次调用它,则应用程序崩溃.Reflection.TargetInvocationException WinRT-Informationen:无法获取摄像头。您只能在前台使用此类。
有人知道这个问题吗?
protected AudioVideoCaptureDevice Device { get; set; }
public Page10()
{
InitializeComponent();
}
async void tglSwitch_Checked(object sender, RoutedEventArgs e)
{
var sensorLocation = CameraSensorLocation.Back;
if (this.Device == null)
{
// get the AudioVideoCaptureDevice
this.Device = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
}
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
{
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
// set flash power to maxinum
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
this.tglSwitch.Content = "Light on";
this.tglSwitch.SwitchForeground = new SolidColorBrush(Colors.Green);
}
}
void tglSwitch_Unchecked(object sender, RoutedEventArgs e)
{
var sensorLocation = CameraSensorLocation.Back;
sensorLocation = CameraSensorLocation.Back;
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off))
{
this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
this.tglSwitch.Content = "Light off";
}
}
答案 0 :(得分:0)
我建议在页面生命周期中使用OpenAsync ONE TIME初始化相机,例如在OnNavigatedTo
事件中。只有让SetProperty()
方法调用复选框事件中的代码才能控制光线。通过调用OnNavigatedFrom
,正确处理相机然后离开页面也是非常重要的,例如在device.Dispose()
事件中。此选项还可使您的手电筒更快地工作。
请记住,Windows Phone 8.1现在有专门用于火炬的API,效果很好,代码更漂亮。您也可以在Silverlight项目中使用,但必须迁移项目。以下是有关http://developer.nokia.com/community/wiki/Using_the_camera_light_in_Windows_Phone_7,_8_and_8.1和https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.devices.torchcontrol的更多信息。