Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

What I have tried:

<combobox x:name="SettingsCamera" horizontalalignment="Stretch" grid.row="1" grid.column="0" margin="0,5" selectionchanged="SettingsCamera_SelectionChanged" xmlns:x="#unknown">

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

}


}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900