我正在制作一个手电筒应用程序,其中我需要在按下 ON 按钮时不断使用相机的 LED 并在按下同一个按钮时将其关闭。我按照这篇文章使用 Reflection 使用摄像机打开 LED。ON/OFF 操作只能正常工作一次。代码如下:
private VideoCamera _videoCamera;
private VideoCameraVisualizer _videoCameraVisualizer;
bool _isFlashOff = true;
private void FlashButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (_isFlashOff)
{
_isFlashOff = false;
// Check to see if the camera is available on the device.
if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
// Use standard camera on back of device.
_videoCamera = new VideoCamera();
// Event is fired when the video camera object has been initialized.
_videoCamera.Initialized += VideoCamera_Initialized;
// Add the photo camera to the video source
_videoCameraVisualizer = new VideoCameraVisualizer();
_videoCameraVisualizer.SetSource(_videoCamera);
}
}
else
{
_isFlashOff = true;
_videoCamera.StopRecording();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void VideoCamera_Initialized(object sender, EventArgs e)
{
_videoCamera.LampEnabled = true;
_videoCamera.StartRecording();
}
由于没有按照文章中指定的 VideoCamera 类中的StopRecording方法实现:使用 Reflection 使用摄像机打开 LED。我将函数设为:
public void StopRecording()
{
// Invoke the stop recording method on the video camera object.
_videoCameraStopRecordingMethod.Invoke(_videoCamera, null);
}
问题是当我再次按下 ON 按钮时,“异常”被抛出为“TargetInvocationException”。我无法找出导致异常的问题。StopRecording() 函数是否正确..??