使用Windows Phone 8.1中的文件选择器选取的图像

时间:2014-07-13 10:43:13

标签: c# windows-phone-8.1

我想在Windows Phone 8.1中使用文件选择器选取的图像。我使用此代码选择了图像

  private async void gallery_Tapped(object sender, TappedRoutedEventArgs e)
    {
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");

        // Launch file open picker and caller app is suspended and may be terminated if required
        openPicker.PickSingleFileAndContinue();
   }


 public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
        {
            if (args.Files.Count > 0)
            {
                StorageFile file=args.Files[0];

                IRandomAccessStream fileStream = await                     file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                BitmapImage bitmapImage = new BitmapImage();

                bitmapImage.SetSource(fileStream);
                MyImage.Source=bitmapImage;

  }
            else
            {

            }
}

ContinueFileOpenPicker没有执行我试过this但是无法理解。可以在这里指导我一步一步我应该怎么做才能使它工作。谢谢

1 个答案:

答案 0 :(得分:4)

 protected override void OnActivated(IActivatedEventArgs args)
        {
            var root = Window.Current.Content as Frame;
            var mainPage = root.Content as MainPage;
            if (mainPage != null && args is FileOpenPickerContinuationEventArgs)
            {
                mainPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs);
            }
        }

将此代码放在app.xaml.cs中它将起作用..