1

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

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

...但它抛出一个异常:`指定的方法不受支持。";

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

为什么这行不通?

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

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

4 回答 4

5

根据 MSDN 论坛和我认为是 MS 员工的答案(这里):

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

所以看起来你被困在了PhotoChooserTask而不是FileOpenPicker

于 2013-03-12T16:50:49.783 回答
5

这确实有效,但仅适用于 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
        }
    }

您也可以抓取多个文件。

最后但最重要的是,您需要在项目中添加一个延续管理器类。这将在从选择器返回时管理应用程序的重新激活。转到此文档以了解如何将 ContinuationManager 添加到项目中(抱歉链接,太多信息放在这里)。

于 2014-06-02T14:32:55.957 回答
1

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

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

于 2013-03-12T09:26:45.513 回答
0

根据文档,提到:最低支持电话:不支持

检查此链接以获取详细信息 http://msdn.microsoft.com/en-us/library/windowsphone/develop/br207852.aspx

于 2014-05-02T10:09:52.690 回答