问题
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.
Here is my code:
<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:
I tested your code with MobileEmulator version 10240, the following image shows what I get:
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.
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.
So maybe what you want is else if (SettingsCamera.SelectedIndex == 1 || SettingsCamera.SelectedIndex == 2 || SettingsCamera.SelectedIndex == 3)
.
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.
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);
}
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.
来源:https://stackoverflow.com/questions/35526206/how-to-set-a-selected-camera-with-combobox-in-windows-10-universal-app