Windows Phone 8.1中的文件选择器

8
我想在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.";
            }
        }

错误

enter image description here


1
你不应该使用PickSingleFileAndContinue吗? - Ulugbek Umirov
1
就像Ulugbek所说的那样 - 您正在针对Windows Phone,因此您不能使用那些方法(PickSingleFileAsync()),因此在选择文件时可能会终止您的应用程序。您必须使用上述方法 - 更多参考和一个很好的示例,您可以在MSDN这里找到。 - Romasz
是的,我纠正了那个问题,但如何处理选择的文件呢?这会返回空值吗? - Ghazanfar Khan
@user3814490 Romasz 已经给了你关于 ContinuationManager 的链接。 - Ulugbek Umirov
我正在努力编写App.xaml.cs OnActivated事件的代码。 - Ghazanfar Khan
4个回答

15

我认为你可以在需要的页面处理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];

    }
}

当您从选择器中选择文件时,将调用上述方法。我相信这会对您有所帮助。


12

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

步骤1:在您的Windows Phone 8.1应用程序中添加图片库功能。

图片库功能

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

文件打开选择器声明

步骤3:在MainPage.xaml中添加按钮和图像。

<Grid>
<Image Name="img"/>
<Button Content="click me" Click="Button_Click"/>
</Grid>

步骤四:添加全局变量视图。

CoreApplicationView view;

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

view = CoreApplication.GetCurrentView();

步骤 5:在按钮单击事件上添加调用文件打开选择器的代码。

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:在视图激活事件中将图像设置为主页面。

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从图片库中选择图片


你的代码中从未使用过ImagePath变量。它是用来做什么的? - Ali Demirci
你说得对!我从未使用过它,我甚至不知道它为什么存在,我正在查看我的当前实现,但ImagePath并不存在。我现在正在编辑我的答案。谢谢! - Roberto Orozco
在检查计数之前,如果用户没有选择文件选择器并中止操作,事件处理程序是否应该被移除? - Cee McSharpface

3

在wp 8.1 xaml中,按钮点击请使用RoutedEventArgs而非TappedRoutedEventArgs。不要使用async关键字。

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

            }
}

  • 在类中的任何位置(方法外)定义一个名为 CoreApplicationView 的全局变量
  • 别忘了在相关页面类的构造函数中,在 InitializeComponent(); 方法后使用 view = CoreApplication.GetCurrentView();

我认为这会有所帮助 :) 谢谢


0

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接