WP7手电筒应用程序错误

时间:2012-09-24 03:20:06

标签: windows-phone-7 flashlight

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

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(...)

来进行初始化

以下是错误的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分: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();
    }
}