UWP FileOpenPicker 的使用注意点

从Winform迁移到UWP之后发现FileOpenDialog这个好用的控件不见了。查了Bing之后才知道在UWP中是用FileOpenPicker了。

下面这个就是微软的示例代码,但是放到我们UWP的事件函数中会报错 ”await 只能用于异步方法中,请考虑使用async修饰方法”

var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");

Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
    // Application now has read/write access to the picked file
    this.textBlock.Text = "Picked photo: " + file.Name;
}
else
{
    this.textBlock.Text = "Operation cancelled.";
}

 报错位置在 “await”所在的行,这种用法我很陌生。捣鼓了半天也没有整通过。

private void PickAFileButton_Click(object sender, RoutedEventArgs e)
        {
            // Clear previous returned file name, if it exists, between iterations of this scenario
            OutputTextBlock.Text = "";

            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null)
            {
                // The StorageFile has read/write access to the picked file.
                // See the FileAccess sample for code that uses a StorageFile to read and write.
                OutputTextBlock.Text = "Picked photo: " + file.Name;
            }
            else
            {
                OutputTextBlock.Text = "Operation cancelled.";
            }
        }

后来找到了一份微软的SampleCode,才整明白。其实解决方法很简单,就像报错提示的一样。在事件函数前面加上“async”就可以了。

private async void PickAFileButton_Click(object sender, RoutedEventArgs e){

 在示例代码中,我们看到获取文件名就直接调用 StorageFile.Name, 但是这个文件名是不包含完整路径的,包含完整路径的文件名是 StorageFile.Path.

在 C# 中,获取用户的相机权限通常是在 Windows Presentation Foundation (WPF) 或 Universal Windows Platform (UWP) 的背景下进行的。以下是 UWP 中获取相机权限的基本步骤: **对于Windows 10 UWP应用:** 1. **添加必要的命名空间:** ```csharp using Windows.Graphics.Imaging; using Windows.Media.Capture; using Windows.Storage.Pickers; ``` 2. **请求授权:** 首先,在页面加载时检查权限。如果没有,使用 `MediaCaptureInitializationSettings` 和 `StorageFilePicker` 来提示用户: ```csharp if (!MediaCapture.IsCameraPresent) return; var settings = new MediaCaptureInitializationSettings(); settings.VideoDeviceId = MediaCapture.GetDefaultVideoDeviceId(); // 设置摄像头 var picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Image; picker.SuggestedStartLocation = PickerLocationId.CameraRoll; picker.FileTypeFilter.Add(".jpg"); // 添加文件类型过滤器 StorageFile file = await picker.PickSingleFileAsync(); if (file != null) { // 用户选择了照片,你可以继续处理 } else { // 用户拒绝了权限 } ``` 如果用户首次打开应用,会弹出一个对话框询问是否允许应用访问相机。 **对于WPF应用(使用Windows.Storage.AccessCache):** - WPF不直接支持相机访问,但你可以使用`StorageAccessManager`来检查权限,然后引导用户到设备设置手动授予权限。 - 由于WPF没有原生支持,你可能需要借助第三方库如MahApps.Metro或者社区驱动的解决方案来实现。 请注意,这些示例假设你的应用已经通过Windows Store发布,并且用户正在运行Windows 10或更新版本。如果在桌面版Win32环境中,权限管理可能更为复杂,因为涉及到不同级别的用户权限。同时,如果你的应用要求更高的隐私权限,确保遵守相应平台的隐私政策和指南。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值