如何在Windows 10 Universal App中使用combobox“设置”所选摄像机

时间:2016-02-20 16:56:05

标签: c# wpf windows win-universal-app

我正在开发Windows应用程序。我想在Windows Universal App或Combobox中使用WPF从前置摄像头切换到后置摄像头。

我已编写了一些内容,但我没有弄到错误的地方。

这是我的代码:

<ComboBox  x:Name="SettingsCamera"  HorizontalAlignment="Stretch" Grid.Row="1" Grid.Column="0" Margin="0,5" SelectionChanged="SettingsCamera_SelectionChanged"/>

private async void InitializeCameraAsync()
        {DeviceInformation device = FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel);



                var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);
                SettingsCamera.Items.Clear();
                //_deviceList = new List<Windows.Devices.Enumeration.DeviceInformation>();
                // Add the devices to deviceList
                if (devices.Count > 0)
                {
                    for (var i = 0; i < devices.Count; i++)
                    {
                       // _deviceList.Add(devices[i]);
                        SettingsCamera.Items.Add(devices[i].Name);
                    }
                }

                else
                {
                    Debug.WriteLine("No camera device is found ");
                }
        }

private async void SettingsCamera_SelectionChanged(object sender,SelectionChangedEventArgs e){
            if (SettingsCamera.SelectedIndex == 0)
            {

                try
                {
                    var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
                    //SettingsMicrophone.Items.Clear();
                    var frontCamera = allVideoDevices.FirstOrDefault(d => d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);


                    if (allVideoDevices.Count == 0)
                    {
                        SettingsCamera.Items.Add(frontCamera.Name);
                    }
                }
                catch (NullReferenceException)
                {
                    //audioExist = false;
                    SettingsCamera.Items.Add("No michrophone on your system");
                }




            }
            else if (SettingsCamera.SelectedIndex == 1 && SettingsCamera.SelectedIndex == 2 && SettingsCamera.SelectedIndex == 3)
            {

                try
                {
                    var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
                    var backCamera = allVideoDevices.FirstOrDefault(d => d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
                    //SettingsMicrophone.Items.Clear();
                    if (allVideoDevices.Count >=2)
                    {
                        SettingsCamera.Items.Add(backCamera.Name);
                    }
                    SettingsCamera.Items.Add(backCamera.Name);

                    //make first cam default
                }
                catch (NullReferenceException)
                {
                    //audioExist = false;
                    SettingsCamera.Items.Add("No michrophone on your system");
                }

            }


        }

1 个答案:

答案 0 :(得分:0)

我使用MobileEmulator版本10240测试了您的代码,下图显示了我得到的内容: enter image description here

就我而言,如果您选择项目&#34; SocCaptureSim RFC&#34;,[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 为0,如果您选择项目&#34; SocCaptureSim FFC&#34;,{{ 1}}是1.

但是根据你的SelectedIndex方法的代码,当我选择项目&#34; SocCaptureSim FFC&#34;时,什么都不会发生,因为你SelectedIndex的条件是{{1} },SettingsCamera_SelectionChanged(object sender,SelectionChangedEventArgs e)不支持多项选择。

所以也许你想要的是else if

您的代码中还存在另外两个问题,SettingsCamera.SelectedIndex == 1 && SettingsCamera.SelectedIndex == 2 && SettingsCamera.SelectedIndex == 3方法ComboBox,尽管您没有根据左侧代码发布else if (SettingsCamera.SelectedIndex == 1 || SettingsCamera.SelectedIndex == 2 || SettingsCamera.SelectedIndex == 3)方法的代码,我想你在这里传递了错误的参数。

DeviceInformation device = FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel);方法中,我可以理解您要查找所有相机设备并将设备添加到InitializeCameraAsync()列表中,但是当您选择索引0时,

FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel)

此代码在此处没有意义,如果InitializeCameraAsync(),此ComboBox将首先导致空指针错误。

由于我没有看到您打开相机拍照或录制的任何代码,您可以参考官方Basic camera app sample,了解如何让相机工作。