我正在开发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");
}
}
}
答案 0 :(得分:0)
我使用MobileEmulator版本10240测试了您的代码,下图显示了我得到的内容:
就我而言,如果您选择项目&#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,了解如何让相机工作。