Windows Phone 8.1中的文件选择器

我想从Windows Phone 8.1中的图片相册中选择一张图片。 为此我使用了这段代码,但它给出了错误

private async void gallery_Tapped(object sender, TappedRoutedEventArgs e) { FileOpenPicker opener = new FileOpenPicker(); opener.ViewMode = PickerViewMode.Thumbnail; opener.SuggestedStartLocation = PickerLocationId.PicturesLibrary; opener.FileTypeFilter.Add(".jpg"); opener.FileTypeFilter.Add(".jpeg"); opener.FileTypeFilter.Add(".png"); StorageFile file = await opener.PickSingleFileAsync(); if (file != null) { // We've now got the file. Do something with it. var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); await bitmapImage.SetSourceAsync(stream); var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream); MyImage.Source=bitmapImage; } else { //OutputTextBlock.Text = "The operation may have been cancelled."; } } 

错误

在此处输入图像描述

我认为即使在您需要的页面中也可以处理OnActivated事件。 像这样的东西

 CoreApplicationView view = CoreApplication.GetCurrentView(); ImagePath=string.Empty; FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; filePicker.ViewMode = PickerViewMode.Thumbnail; // Filter to include a sample subset of file types filePicker.FileTypeFilter.Clear(); filePicker.FileTypeFilter.Add(".bmp"); filePicker.FileTypeFilter.Add(".png"); filePicker.FileTypeFilter.Add(".jpeg"); filePicker.FileTypeFilter.Add(".jpg"); filePicker.PickSingleFileAndContinue(); view.Activated += viewActivated; private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1) { FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs; if (args != null) { if (args.Files.Count == 0) return; view.Activated -= viewActivated; storageFileWP = args.Files[0]; } } 

从选择器中选择文件时,将调用上述方法。 我相信它可以帮助你。

使用Windows Phone 8.1中的FileOpenPicker从图片库中选择图片。

步骤1:在Windows Phone 8.1应用程序中添加图片库function。

图片库功能

第2步:添加文件打开选择器作为声明。

文件打开选择器声明

第3步:向MainPage.xaml添加一个按钮和图像。

     

第4步:添加全局变量视图。

 CoreApplicationView view; 

步骤4.1在页面构造函数中初始化。

 view = CoreApplication.GetCurrentView(); 

步骤5:添加代码以在Button Click事件上调用File Open Picker。

 private void Button_Click(object sender, RoutedEventArgs e) { FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; filePicker.ViewMode = PickerViewMode.Thumbnail; // Filter to include a sample subset of file types filePicker.FileTypeFilter.Clear(); filePicker.FileTypeFilter.Add(".bmp"); filePicker.FileTypeFilter.Add(".png"); filePicker.FileTypeFilter.Add(".jpeg"); filePicker.FileTypeFilter.Add(".jpg"); filePicker.PickSingleFileAndContinue(); view.Activated += viewActivated; } 

步骤6:在View激活的事件上将图像设置为MainPage。

 private async void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1) { FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs; if (args != null) { if (args.Files.Count == 0) return; view.Activated -= viewActivated; StorageFile storageFile = args.Files[0]; var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read); var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); await bitmapImage.SetSourceAsync(stream); var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream); img.Source=bitmapImage; } } 

它还允许您拍照并使用它。

参考: 使用Windows Phone 8.1中的FileOpenPicker从图片库中选择图片

使用RoutedEventArgs而不是TappedRoutedEventArgs在wp 8.1 xaml中单击按钮不要使用异步关键字

 private void OpenImageFile(object sender, RoutedEventArgs e) { FileOpenPicker filePicker = new FileOpenPicker(); filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; filePicker.ViewMode = PickerViewMode.Thumbnail; // Filter to include a sample subset of file types filePicker.FileTypeFilter.Clear(); filePicker.FileTypeFilter.Add(".bmp"); filePicker.FileTypeFilter.Add(".png"); filePicker.FileTypeFilter.Add(".jpeg"); filePicker.FileTypeFilter.Add(".jpg"); filePicker.PickSingleFileAndContinue(); view.Activated += viewActivated; } private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1) { FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs; if (args != null) { if (args.Files.Count == 0) return; view.Activated -= viewActivated; StorageFile SelectedImageFile = args.Files[0]; } } 

var fill = await StorageFile.GetFileFromPathAsync(selectItem.FolderPath); BitmapImage bit = new BitmapImage();

  if (fill != null) { // We've now got the file. Do something with it. var stream = await fill.OpenAsync(Windows.Storage.FileAccessMode.Read); //var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); //await bitmapImage.SetSourceAsync(stream); bit.SetSource(stream); imgTeste.Source = bit; pvMestre.SelectedIndex = 1; } else { //OutputTextBlock.Text = "The operation may have been cancelled."; }