简体   繁体   English

Windows Phone 8.1 中的文件选取器

[英]File Picker in Windows Phone 8.1

I want to pick an image from my pictures album in windows phone 8.1 .我想从 Windows Phone 8.1 的相册中选择一张图片。 For this I used this code but its gives error为此,我使用了这段代码,但它给出了错误

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

Error错误

在此处输入图片说明

I think you can handle the OnActivated event even in the page where you required.我认为您甚至可以在您需要的页面中处理 OnActivated 事件。 Something like this像这样的东西

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

    }
}

When you select the files from the picker the above method will be called.当您从选择器中选择文件时,将调用上述方法。 I believe it helps you.我相信它对你有帮助。

Using FileOpenPicker in Windows Phone 8.1 to choose picture from Picture Gallery.在 Windows Phone 8.1 中使用 FileOpenPicker 从图片库中选择图片。

Step 1: Add Picture Library Capability in your Windows Phone 8.1 app.步骤 1:在您的 Windows Phone 8.1 应用程序中添加图片库功能。

图片库功能

Step 2: Add File Open Picker as a declaration.第 2 步:添加 File Open Picker 作为声明。

文件打开选取器声明

Step 3: Add a button and image to MainPage.xaml.步骤 3:向 MainPage.xaml 添加按钮和图像。

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

Step 4: Add global variable view.第四步:添加全局变量视图。

CoreApplicationView view;

Step 4.1 Initialize in page constructor.步骤 4.1 在页面构造函数中初始化。

view = CoreApplication.GetCurrentView();

Step 5: Add the code to call the File Open Picker on Button Click event.第 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; 
}

Step 6: On View activated event set the image to the MainPage.第 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;
  }
}

It also allows you to take a photo and use it.它还允许您拍照并使用它。

Reference: Using FileOpenPicker in Windows Phone 8.1 to choose picture from Picture Gallery参考: 在 Windows Phone 8.1 中使用 FileOpenPicker 从图片库中选择图片

Use RoutedEventArgs instead of TappedRoutedEventArgs for button click in wp 8.1 xaml Don't use async key word使用 RoutedEventArgs 而不是 TappedRoutedEventArgs 在 wp 8.1 xaml 中单击按钮 不要使用 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]; } }

  • And use CoreApplicationView view;并使用 CoreApplicationView 视图; Any where in the class out side of every method as global类中每个方法之外的任何位置作为全局
  • Don't forget to use view = CoreApplication.GetCurrentView();不要忘记使用 view = CoreApplication.GetCurrentView(); inside the constructor of the relevant page class after InitializeComponent();在 InitializeComponent() 之后的相关页面类的构造函数内; method方法

I think this will help :) Thanks我认为这会有所帮助:) 谢谢

var fill = await StorageFile.GetFileFromPathAsync(selectItem.FolderPath); var fill = await StorageFile.GetFileFromPathAsync(selectItem.FolderPath); BitmapImage bit = new BitmapImage(); 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.";
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM