I have created a very basic MauiApp because I wanted to try the MediaPicker on the Windows Platform.
Thus I copied the code from the official documentation and tried to run my application
However if I add <uap:Capability Name="webcam"/> to the Package.appxmanifest file as suggested in the documentaion, and run the application it gives me the following error:
Error DEP0700: Registration of the app failed. [0x80080204] error 0xC00CE169: App
manifest validation error: The app manifest must be valid as per schema: Line 39, Column
21, Reason: 'webcam' violates enumeration constraint of 'documentsLibrary
picturesLibrary videosLibrary musicLibrary enterpriseAuthentication
sharedUserCertificates userAccountInformation removableStorage appointments contacts
phoneCall blockedChatMessages objects3D voipCall chat'.
The attribute 'Name' with value 'webcam' failed to parse. MauiApp3
So in order to solve this problem I tried to change the capability from <uap:Capability Name="webcam"/> to <DeviceCapability Name="webcam"/>.
In this way I can run the application without errors, but photo is always null:
public async void TakePhoto(object sender, EventArgs e)
{
if (MediaPicker.Default.IsCaptureSupported)
{
FileResult photo = await MediaPicker.Default.CapturePhotoAsync();
if (photo != null)
{
// save the file into local storage
string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
using Stream sourceStream = await photo.OpenReadAsync();
using FileStream localFileStream = File.OpenWrite(localFilePath);
await sourceStream.CopyToAsync(localFileStream);
}
else
{
// *** IT ALWAYS ENTERS IN THE ELSE CLAUSE ***
// *** BECAUSE photo IS ALWAYS NULL ***
CounterBtn.Text = $"Capture is supported but {photo} is null";
}
}
}
Note: The function above is called when I click to this button that I've defined in MainPage.xaml file:
<Button
x:Name="ImageBtn"
Text="Take Photo"
SemanticProperties.Hint="Take Image"
Clicked="TakePhoto"
HorizontalOptions="Center" />