懐中電灯アプリを作っています。ONボタンを押すとカメラのLEDを常に使用し、同じボタンを押すとカメラのLEDをオフにする必要があります。私はこの記事に従いました。Reflectionを使用してビデオカメラでLEDをオンにします。ON/OFF操作は1回だけ正常に動作します。コードは次のとおりです。
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ボタンを押すと「Exception」が「TargetInvocationException」としてスローされることです。例外の原因となる問題を特定できません。StopRecording()関数は正しいですか。