OpenFilePicker无法在Windows Phone 8上运行(不支持指定的方法)
我试图使用以下方法选择一个文件:
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; } }
…但它抛出exception:`不支持指定的方法。“;
我复制并粘贴了Windows Phone 8文档中的代码。 他们的样本都不起作用。 我想也许我错过了一个文档function/合同或其他什么,但他们甚至不存在于VS for Phone应用程序中。
为什么这不起作用?
我已将其追踪到尝试的第一行:
FileOpenPicker openPicker = new FileOpenPicker(); // this is the line the exception is thrown on.
根据MSDN论坛和我认为是MS员工的答案( 这里 ):
我们目前不支持选择照片以外的文件或从其他商店应用程序中选择文件。
所以看起来你会PhotoChooserTask
而不是FileOpenPicker
。
这确实有效,但仅适用于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类。 这将管理从选择器返回时应用程序的重新激活。 转到此文档以了解如何将ContinuationManager添加到项目中(抱歉链接,此处放置了太多信息)。
您只能使用本机应用程序中的FileOpenPicker
,例如Direct3D应用程序。
您可以使用PhotoChooserTask从图片中心选择图片。
根据文档,提到:支持的最低电话:不支持
查看此链接了解详细信息http://msdn.microsoft.com/en-us/library/windowsphone/develop/br207852.aspx