Jam*_*mes 1 .net c# xaml windows-phone-8 fileopenpicker
我试图使用以下方法选择一个文件:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
...但它抛出一个异常:`不支持指定的方法.";
我复制并粘贴了Windows Phone 8文档中的代码.他们的样本都不起作用.我想也许我错过了一个文档功能/合同或其他什么,但它们甚至不存在于VS for Phone应用程序中.
为什么这不起作用?
我已将其追踪到尝试的第一行:
FileOpenPicker openPicker = new FileOpenPicker(); // this is the line the exception is thrown on.
Run Code Online (Sandbox Code Playgroud)
这确实有效,但仅适用于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();
Run Code Online (Sandbox Code Playgroud)
添加此方法以处理所选照片:
public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
if (args.Files.Count > 0)
{
var userChosenbPhoto = args.Files[0].Name;
}
else
{
//user canceled picker
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以抓取多个文件.
最后但最重要的是,您需要在项目中添加continuation manager类.这将管理从选择器返回时应用程序的重新激活.转到此文档以了解如何将ContinuationManager添加到项目中(抱歉链接,此处放置了太多信息).
归档时间: |
|
查看次数: |
4197 次 |
最近记录: |