【问题标题】:WP7 Flashlight Application ErrorWP7 手电筒应用错误
【发布时间】:2012-09-24 03:20:05
【问题描述】:

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

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(...) 来进行初始化

这是错误的截图:

【问题讨论】:

    标签: windows-phone-7 flashlight


    【解决方案1】:

    其实没那么容易... 可以通过 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();
        }
    }
    

    【讨论】:

    • 什么错误?这不包含所有代码,但想法就在这里:您必须使用 PhotoCamera 和 VideoBrush。
    • 我已将照片相机添加到视频源,但我一直收到同样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    相关资源
    最近更新 更多