2

在这个应用程序中有两个按钮来打开和关闭相机手电筒。我正在使用此代码,但出现错误。

using Microsoft.Devices;

public partial class MainPage : PhoneApplicationPage
{

    PhotoCamera cam = new PhotoCamera(CameraType.FrontFacing); 

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender, RoutedEventArgs e)
    {
        cam.FlashMode = FlashMode.On;  
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        cam.FlashMode = FlashMode.Off;
    }
}

我收到此错误:

System.InvalidOperationException 未处理 Message=在完全初始化之前,您不能使用此实例。您可以通过将此 Camera 对象传递给 VideoBrush.SetSource(...)

这是错误的屏幕截图:

在此处输入图像描述

4

1 回答 1

0

其实没那么容易……手机上的闪光灯可以通过PhotoCamera对象来访问。所以我尝试的第一件事就是以这种方式打开闪光灯。要使用照片相机,您需要两个对象 PhotoCamera 和 VideoBrush。

private PhotoCamera _photoCamera;
private VideoBrush _videoBrush;

// Check to see if the camera is available on the device.
if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
    // Use standard camera on back of device.
    _photoCamera = new PhotoCamera(CameraType.Primary);

    // Event is fired when the PhotoCamera object has been initialized.
    _photoCamera.Initialized += PhotoCamera_Initialized;

    // Add the photo camera to the video source
    _videoBrush = new VideoBrush();
    _videoBrush.SetSource(_photoCamera);
}

private void PhotoCamera_Initialized(object sender, CameraOperationCompletedEventArgs e)
{
    // Check if flash mode is supported on the device.
    if (_photoCamera.IsFlashModeSupported(FlashMode.On))
    {
        // Turn the flash on.
        _photoCamera.FlashMode = FlashMode.On;
        _photoCamera.Focus();
    }
}
于 2012-09-24T09:39:59.410 回答