Windows平台错误上的MAUI媒体选择器

发布于 2025-02-03 18:28:54 字数 2289 浏览 4 评论 0原文

我创建了一个非常基本的Mauiapp,因为我想在Windows平台上尝试MediaPicker。

因此,我从官方文档并尝试运行我的应用程序

,但是,如果我添加< uap:功能名称=“ webCam”/>package> package.appxmanifest file file如文档中所建议的,并运行应用程序给我以下错误:

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            
        

因此,为了解决此问题,我尝试将功能从< uap:功能名称=“ webcam”/> to < devicecapibility name =“ WebCam”/>

这样,我可以无错误地运行应用程序,但是photo始终为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";
        }
    }
}

注意:当我单击我在mainpage.xaml <我单击此按钮时,上述函数被调用。 /代码>文件:

        <Button 
            x:Name="ImageBtn"
            Text="Take Photo"
            SemanticProperties.Hint="Take Image"
            Clicked="TakePhoto"
            HorizontalOptions="Center" />

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 = 
quot;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" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡紫姑娘! 2025-02-10 18:28:54

不幸的是,这是Winui3的古老而已知的错误。
microsoft/windowsappsdk#1034

作为一种可行的方法,您可以从用于Android,iOS和催化剂的毛伊岛。

然后,为Windows实现自定义媒体挑选器,并在所有capture ...方法中使用capture> capturephotoasync使用自定义类用于摄像机捕获:

public async Task<FileResult> CaptureAsync(MediaPickerOptions options, bool photo)
{
    var captureUi = new CameraCaptureUI(options);

    var file = await captureUi.CaptureFileAsync(photo ? CameraCaptureUIMode.Photo : CameraCaptureUIMode.Video);

    if (file != null)
        return new FileResult(file.Path,file.ContentType);

    return null;
}

这是新的camerAcaptureui class:

public async Task<StorageFile> CaptureFileAsync(CameraCaptureUIMode mode)
{
    var extension = mode == CameraCaptureUIMode.Photo ? ".jpg" : ".mp4";

    var currentAppData = ApplicationData.Current;
    var tempLocation = currentAppData.LocalCacheFolder;
    var tempFileName = $"CCapture{extension}";
    var tempFile = await tempLocation.CreateFileAsync(tempFileName, CreationCollisionOption.GenerateUniqueName);
    var token = Windows.ApplicationModel.DataTransfer.SharedStorageAccessManager.AddFile(tempFile);

    var set = new ValueSet();
    if (mode == CameraCaptureUIMode.Photo)
    {
        set.Add("MediaType", "photo");
        set.Add("PhotoFileToken", token);
    }
    else
    {
        set.Add("MediaType", "video");
        set.Add("VideoFileToken", token);
    }

    var uri = new Uri("microsoft.windows.camera.picker:");
    var result = await Windows.System.Launcher.LaunchUriForResultsAsync(uri, _launcherOptions, set);
    if (result.Status == LaunchUriStatus.Success && result.Result != null)
    {
        return tempFile;
    }

    return null;
}

也在package.appxmanifest中添加以下功能:

<DeviceCapability Name="microphone"/>
<DeviceCapability Name="webcam"/>

摄像头类的学分

编辑:
修改的代码允许视频捕获,

Unfortunately this is a old and known bug for WinUI3.
microsoft/WindowsAppSDK#1034

As a workaround you can create a custom mediapicker that inherit everything from the MAUI one for Android, iOS and Catalyst.

Then implement a custom mediapicker for Windows, intheriting all the Capture... methods and reimplementing the CapturePhotoAsync using a custom class for camera capture:

public async Task<FileResult> CaptureAsync(MediaPickerOptions options, bool photo)
{
    var captureUi = new CameraCaptureUI(options);

    var file = await captureUi.CaptureFileAsync(photo ? CameraCaptureUIMode.Photo : CameraCaptureUIMode.Video);

    if (file != null)
        return new FileResult(file.Path,file.ContentType);

    return null;
}

This is the new CameraCaptureUI class:

public async Task<StorageFile> CaptureFileAsync(CameraCaptureUIMode mode)
{
    var extension = mode == CameraCaptureUIMode.Photo ? ".jpg" : ".mp4";

    var currentAppData = ApplicationData.Current;
    var tempLocation = currentAppData.LocalCacheFolder;
    var tempFileName = 
quot;CCapture{extension}";
    var tempFile = await tempLocation.CreateFileAsync(tempFileName, CreationCollisionOption.GenerateUniqueName);
    var token = Windows.ApplicationModel.DataTransfer.SharedStorageAccessManager.AddFile(tempFile);

    var set = new ValueSet();
    if (mode == CameraCaptureUIMode.Photo)
    {
        set.Add("MediaType", "photo");
        set.Add("PhotoFileToken", token);
    }
    else
    {
        set.Add("MediaType", "video");
        set.Add("VideoFileToken", token);
    }

    var uri = new Uri("microsoft.windows.camera.picker:");
    var result = await Windows.System.Launcher.LaunchUriForResultsAsync(uri, _launcherOptions, set);
    if (result.Status == LaunchUriStatus.Success && result.Result != null)
    {
        return tempFile;
    }

    return null;
}

Also in the Package.appxmanifest add these capabilities:

<DeviceCapability Name="microphone"/>
<DeviceCapability Name="webcam"/>

Credits for the CameraCapture class

Edit:
Modified code to allow video capturing, credits here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文