我想选择一个文件使用:
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应用程序。
为什么这个不行?
我一直追踪到try的第一行:
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
}
}
你也可以抓取多个文件。
最后但最重要的是,您需要在项目中添加一个延续管理器类。这将管理从选择器返回时应用程序的重新激活。请参阅本文档,了解如何将ContinuationManager添加到项目中(很抱歉提供了一个链接,这里的信息太多了)。您只能使用本地应用程序中的FileOpenPicker
,如Direct3D。
你可以使用PhotoChooserTask从图片中心选择图片
根据文档,提到:最小支持电话:None supported
查看此链接了解详细信息http://msdn.microsoft.com/en-us/library/windowsphone/develop/br207852.aspx