如何“设置"Windows 10 通用应用程序中带有组合框的选定相机 [英] How to "set" a selected camera with combobox in Windows 10 Universal App
问题描述
我正在开发 Windows 应用程序.我想使用 Windows 通用应用程序或 WPF
中的 Combobox
从前置摄像头切换到后置摄像头.
I am developing windows application.I want to switch from Front Camera to Back Camera with a Combobox
in Windows Universal App or in WPF
.
我已经编写了一些代码,但我不知道我哪里出错了.
I have coded something but I don't get where I made a mistake.
这是我的代码:
<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");
}
}
}
推荐答案
我使用 MobileEmulator 10240 版测试了您的代码,下图显示了我得到的结果:
I tested your code with MobileEmulator version 10240, the following image shows what I get:
在我这边,如果您选择SocCaptureSim RFC"项,SelectedIndex
为 0,如果您选择SocCaptureSim FFC"项,SelectedIndex
为1.
By my side, if you select the item "SocCaptureSim RFC", the SelectedIndex
is 0, and if you select the item "SocCaptureSim FFC", the SelectedIndex
is 1.
但是根据你的 SettingsCamera_SelectionChanged(object sender,SelectionChangedEventArgs e)
方法的代码,当我选择项目SocCaptureSim FFC"时,什么都不会发生,因为你的 else if
是 SettingsCamera.SelectedIndex == 1 &&SettingsCamera.SelectedIndex == 2 &&SettingsCamera.SelectedIndex == 3
,ComboBox
不支持多选.
But according to your code of SettingsCamera_SelectionChanged(object sender,SelectionChangedEventArgs e)
method, when I select the item "SocCaptureSim FFC", nothing will happen, because your condition of the else if
is SettingsCamera.SelectedIndex == 1 && SettingsCamera.SelectedIndex == 2 && SettingsCamera.SelectedIndex == 3
, a ComboBox
doesn't support mutil-selection.
所以也许你想要的是else if (SettingsCamera.SelectedIndex == 1 || SettingsCamera.SelectedIndex == 2 || SettingsCamera.SelectedIndex == 3)
.
您的代码中还有另外两个问题,DeviceInformation device = FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel);
在 InitializeCameraAsync()
方法中,尽管您FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel)
方法的代码没有贴出来,根据左边的代码,我觉得你这里传错参数了.
There are two other problems in your code, DeviceInformation device = FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel);
in the InitializeCameraAsync()
method, although you didn't post the code of FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel)
method, according to the left code, I think you pass the wrong parameter here.
并且在InitializeCameraAsync()
方法中,我可以理解你想找到所有的相机设备并将设备添加到ComboBox
的列表中,但是当你选择索引 0,
And in the InitializeCameraAsync()
method, I can understand that you want to find all the camera device and add the device to the list of ComboBox
, but when you select index 0,
if (allVideoDevices.Count == 0)
{
SettingsCamera.Items.Add(frontCamera.Name);
}
这段代码在这里没有意义,如果allVideoDevices.Count == 0
,这个var frontCamera = allVideoDevices.FirstOrDefault(d => d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
首先会导致空指针错误.
This code make no sense here, if allVideoDevices.Count == 0
, this var frontCamera = allVideoDevices.FirstOrDefault(d => d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
will cause an null pointer error at first.
由于我没有看到你的任何打开相机拍照或记录的代码,你可以参考官方基本相机应用示例,了解如何让相机工作.
Since I didn't see any of your code for opening a camera to take photos or record, you can refer to the official Basic camera app sample, see how to get a camera to work.
这篇关于如何“设置"Windows 10 通用应用程序中带有组合框的选定相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!