C# 如何在按钮点击事件中在前摄像头和主摄像头之间切换

C# 如何在按钮点击事件中在前摄像头和主摄像头之间切换,c#,windows-phone-7,windows-phone-8,camera,C#,Windows Phone 7,Windows Phone 8,Camera,我正在使用PhotoCamera类填充视频画笔并在应用程序中拍照。默认情况下,我将相机设置为正面。但是,我希望能够在主摄像头和前置摄像头之间切换(如果两者都存在)。我为此设置了一个按钮,但我不确定如何正确实现。到目前为止,我掌握的情况如下 MainPage.xaml.cs protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); //Set

我正在使用PhotoCamera类填充视频画笔并在应用程序中拍照。默认情况下,我将相机设置为正面。但是,我希望能够在主摄像头和前置摄像头之间切换(如果两者都存在)。我为此设置了一个按钮,但我不确定如何正确实现。到目前为止,我掌握的情况如下

MainPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        //Set up the Camera
        if (null == camera)
        {
            InitializeCamera();
        }
    }

private void InitializeCamera()
    {
        // Check to see if the camera is available on the device.
        if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) || (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) == true))
        {
            // Initialize the camera, when available.
            if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing))
            {
                // Use front-facing camera if available.
                camera = new Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);
            }
            else
            {
                // Otherwise, use standard camera on back of device.
                camera = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
            }

            camera.Initialized += camera_Initialized;

            //Event is fired when the button is half pressed
            CameraButtons.ShutterKeyHalfPressed += camera_ButtonHalfPress;

            //Event is fired when the button is fully pressed
            CameraButtons.ShutterKeyPressed += camera_ButtonFullPress;

            //Event is fired when the capture sequence is complete and an image is available.
            camera.CaptureImageAvailable += camera_CaptureImageAvailable;

            camera.CaptureCompleted += camera_CaptureCompleted;

            //Set the VideoBrush source to the camera
            videoBrush.SetSource(camera);
        }
    }

private void UninitializeCamera()
    {
        camera.Dispose();

        CameraButtons.ShutterKeyPressed -= camera_ButtonFullPress;
        CameraButtons.ShutterKeyHalfPressed -= camera_ButtonHalfPress;
        camera.CaptureImageAvailable -= camera_CaptureImageAvailable;

        camera = null;
    }

void sensor_Click(object sender, EventArgs e)
    {
        if (null != camera)
            {
                //Determine what cameras exist, and switch if able to
            }
    }

我的主要问题是,考虑到我现有的设置,完成这项任务的最佳方法是什么?有更好的选择吗

可以通过单击按钮重新初始化摄像头,或者,如果需要快速切换,可以设置两个VideoBrush组件并始终隐藏其中一个。不确定这是否会加快速度,但可能值得一试。