【问题标题】:OpenFilePicker not working on Windows Phone 8 (Specified method is not supported)OpenFilePicker 在 Windows Phone 8 上不起作用(不支持指定的方法)
【发布时间】:2013-03-12 09:21:43
【问题描述】:

我正在尝试使用以下方法选择一个文件:

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    try
    {
            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)
            {
                // Application now has read/write access to the picked file
                txt.Text = "Picked file: " + file.Name;
            }
            else
            {
                txt.Text = "Operation cancelled.";
            }

    }
    catch (Exception exception)
    {
        txt.Text = exception.Message;
    }
}

...但是它会抛出异常:`Specified method is not supported.";

我从 Windows Phone 8 文档中复制并粘贴了代码。他们的样品都不起作用。我想也许我缺少文档功能/合同或其他任何东西,但它们甚至不存在于 VS for Phone 应用程序中。

为什么这不起作用?

我已经追踪到尝试的第一行:

FileOpenPicker openPicker = new FileOpenPicker(); // this is the line the exception is thrown on.

【问题讨论】:

  • 使用 AndContinue() 代替 PickSingleFileAsync。有关如何使用它的详细信息,请参阅我的答案。出于对手机内存的考虑,windows Phone 团队需要像这样实现它。同时打开文件选择器应用程序和您的应用程序是不可行的。相反,您的应用会暂停,文件选择器会打开,然后您的应用会恢复。

标签: c# .net xaml windows-phone-8 fileopenpicker


【解决方案1】:

根据 MSDN 论坛和我认为是 MS 员工 (here) 的回答:

我们目前不支持选择照片以外的文件或 从其他商店应用中选择文件。

所以看起来你被PhotoChooserTask而不是FileOpenPicker卡住了。

【讨论】:

  • 我打算对此投反对票只是因为我不同意他们不允许我们选择其他文件类型的决定。哈哈。但我没有。 +1 - 至少我现在知道了。
【解决方案2】:

这确实有效,但仅适用于 Windows Phone 8.1 (Windows Phone) 而不是 Windows Phone 8.0/8.1 (Windows Phone Silverlight)。

代码如下:

FileOpenPicker singleFilePicker = new FileOpenPicker();
        singleFilePicker.ViewMode = PickerViewMode.Thumbnail;
        singleFilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        singleFilePicker.FileTypeFilter.Add(".jpg");
        singleFilePicker.FileTypeFilter.Add(".jpeg");
        singleFilePicker.FileTypeFilter.Add(".png");
        singleFilePicker.PickSingleFileAndContinue();

添加此方法来处理所选照片:

public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
    {
        if (args.Files.Count > 0)
        {
            var userChosenbPhoto = args.Files[0].Name;
        }
        else
        {
            //user canceled picker
        }
    }

您也可以抓取多个文件。

最后但最重要的是,您需要为您的项目添加一个continuation manager 类。这将在从选择器返回时管理应用程序的重新激活。 Go to this documentation to learn how to add a ContinuationManager 到项目(抱歉链接,太多信息放在这里)。

【讨论】:

    【解决方案3】:

    您只能使用来自本机应用程序的FileOpenPicker,例如 Direct3D 应用程序。

    您可以改用PhotoChooserTask 从图片中心选择图片。

    【讨论】:

    • What the?!... 那么我们如何让用户选择一个文件呢? :-/ 我们至少有某种目录/文件查找器吗?
    • 等等...但是,如果您需要使用 C++ 和 Direct3D,为什么还有 C# 的文档呢?
    • 这些 C# 示例适用于 Windows 8,而不是 WP8!我已经更新了答案
    【解决方案4】:

    根据文档,提到: 最低支持手机:不支持

    查看此链接了解详情 http://msdn.microsoft.com/en-us/library/windowsphone/develop/br207852.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 2013-06-18
      相关资源
      最近更新 更多