Windows phone 7 WP7手电筒应用程序错误

Windows phone 7 WP7手电筒应用程序错误,windows-phone-7,flashlight,Windows Phone 7,Flashlight,在该应用程序中,有两个按钮用于打开和关闭相机手电筒。 我正在使用此代码,但我得到一个错误 using Microsoft.Devices; public partial class MainPage : PhoneApplicationPage { PhotoCamera cam = new PhotoCamera(CameraType.FrontFacing); // Constructor public MainPage() { Ini

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

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=在完全初始化此实例之前,无法使用它。您可以通过将此摄影机对象传递给VideoBrush.SetSource进行初始化

以下是错误的屏幕截图:


事实上,这不是那么容易。。。 可以通过PhotoCamera对象访问手机上的闪光灯。所以我第一件事就是用这种方式打开闪光灯。要使用照相相机,你需要两个物体,一个是照相机,另一个是视频刷

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();
    }
}

事实上,这不是那么容易。。。 可以通过PhotoCamera对象访问手机上的闪光灯。所以我第一件事就是用这种方式打开闪光灯。要使用照相相机,你需要两个物体,一个是照相机,另一个是视频刷

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();
    }
}

什么错误?这并没有包含所有的代码,但这里的想法是:你必须使用PhotoCamera和VideoBrush。我已经将照相/摄像机添加到视频源中,但我不断得到相同的错误。什么错误?这并没有包含所有的代码,但这里的想法是:你必须使用PhotoCamera和VideoBrush。我已经将照相/摄像机添加到视频源中,但我不断得到相同的错误。