【问题标题】:How to "set" a selected camera with combobox in Windows 10 Universal App如何在 Windows 10 通用应用程序中使用组合框“设置”选定的相机
【发布时间】:2016-02-20 16:56:05
【问题描述】:

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

我编写了一些代码,但我不知道哪里出错了。

这是我的代码:

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

            }


        }

【问题讨论】:

  • 有点不清楚您的意图是什么。您是否想知道如何在所选项目更改或以编程方式更改 ComboBox 的所选项目时设置属性?您能否澄清一下您所说的“使用组合框设置选定的相机”的意思
  • 使用此代码我正在填充我的组合框:在这种情况下,我得到两个摄像头,一个是前置摄像头和后置摄像头,具体取决于您使用 Surface 或笔记本电脑的设备。现在,如果我在 Combobox 中选择 FirstItem,我想将相机更改为 FrontCamera 并设置该值,如果我在 Combobox 中选择 SecondItem,我想从 FrontCamer 更改并设置 BackCamera 值。
  • 换句话说 var frontCamera = allVideoDevices.FirstOrDefault(d => d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);没有根据 linq 查询中的条件从 allVideoDevices 返回对象?对吗?
  • 是的,这是真的,当我在组合框列表中选择此相机时,该值未设置或未获取。您在 Initalization () 上方还有一个组合框代码;函数和事件组合处理程序我无法理解它是如何完成的......

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


【解决方案1】:

我使用 MobileEmulator 版本 10240 测试了您的代码,下图显示了我得到的结果:

在我这边,如果选择“SocCaptureSim RFC”,SelectedIndex 为 0,如果选择“SocCaptureSim FFC”,SelectedIndex 为 1。

但是根据你SettingsCamera_SelectionChanged(object sender,SelectionChangedEventArgs e)方法的代码,当我选择“SocCaptureSim FFC”项时,什么都不会发生,因为你的else if条件是SettingsCamera.SelectedIndex == 1 &amp;&amp; SettingsCamera.SelectedIndex == 2 &amp;&amp; SettingsCamera.SelectedIndex == 3ComboBox不支持mutil -选择。

所以也许你想要的是else if (SettingsCamera.SelectedIndex == 1 || SettingsCamera.SelectedIndex == 2 || SettingsCamera.SelectedIndex == 3)

你的代码还有两个问题,InitializeCameraAsync()方法中的DeviceInformation device = FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel);,虽然你没有贴出FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel)方法的代码,根据左边的代码,我觉得你这里传错了参数.

而在InitializeCameraAsync()方法中,我可以理解你想找到所有的摄像头设备并将设备添加到ComboBox的列表中,但是当你选择索引0时,

if (allVideoDevices.Count == 0)
{
    SettingsCamera.Items.Add(frontCamera.Name);
}

这段代码在这里没有意义,如果allVideoDevices.Count == 0,这个var frontCamera = allVideoDevices.FirstOrDefault(d =&gt; d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);一开始会导致空指针错误。

由于我没有看到你打开摄像头拍照或录像的代码,你可以参考官方Basic camera app sample,看看如何让摄像头工作。

【讨论】:

  • 感谢您的解释。我的观点是如何通过 Combobox 更改它,因为我设法打开相机并记录它,但我的观点是我不知道如何通过组合框切换相机,应该怎么做我放置在此代码中,而不是-> if (allVideoDevices.Count == 0) { SettingsCamera.Items.Add(frontCamera.Name); }
  • @user2478825,我只指出了您哪里出错了,因为录制视频时还需要考虑其他情况,例如相机的方向。这里的代码可能有点复杂,所以我没有直接给你代码,我只是为你提供了一个官方示例,请看一下这个示例。使用ComboBox切换摄像头并不是什么难事,比如找到后置摄像头:var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多