2014-07-13 15 views
8

Chcę wybrać obraz z albumu ze zdjęciami w systemie Windows 8.1. Do tego użyłem tego kodu, ale jej daje błądSelektor plików w systemie 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."; 
      } 
     } 

Error

enter image description here

+1

nie powinniśmy używać [PickSingleFileAndContinue] (http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.pickers.fileopenpicker.picksinglefileandcontinue. aspx?)? –

+1

Tak jak powiedział Ulugbek - celujesz w Windows Phone i nie możesz używać tych metod (* PickSingleFileAsync() *), dlatego Twoja aplikacja może zostać zakończona podczas wybierania pliku. Musisz użyć powyższej metody - więcej odniesienia i dobry przykład znajdziesz [tutaj na MSDN] (http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn614994.aspx). – Romasz

+0

Tak, poprawiam to, ale jak pracować z tym wybranym plikiem, który zwraca void? –

Odpowiedz

15

myślę, że można obsługiwać OnActivated imprezę nawet na stronie, gdzie wymagane. Coś podobnego to

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

    } 
} 

Po wybraniu plików z selektora zostanie wywołana powyższa metoda. Wierzę, że to ci pomaga.

3

Stosować RoutedEventArgs zamiast TappedRoutedEventArgs do kliknięcia przycisku w WP 8.1 XAML Nie używaj asynchronicznej słowo kluczowe

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

 
      } 
 
}

  • I skorzystać z widoku CoreApplicationView; Dowolna pozycja w klasie każdej metody jako globalna
  • Nie zapomnij użyć view = CoreApplication.GetCurrentView(); wewnątrz konstruktora odpowiedniej klasy strony po InitializeComponent(); Metoda

Myślę, że to pomoże :) Dzięki

0

var napełnienia = czekają 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."; 
        } 
+0

Czy możesz dodać wyjaśnienie do swojego kodu? –

12

Używanie FileOpenPicker w Windows Phone 8.1 do wybierania obrazu z Galerii obrazów.

Krok 1: Dodaj funkcję biblioteki obrazów w aplikacji Windows Phone 8.1.

Picture Library Capability

Krok 2: Otwórz plik Picker Dodaj jako zgłoszenia.

File Open Picker declaration

Krok 3: Dodaj przycisk i wizerunku do MainPage.xaml.

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

Krok 4: Dodaj widok zmiennej globalnej.

CoreApplicationView view; 

Krok 4.1 Inicjalizuj w konstruktorze stron.

view = CoreApplication.GetCurrentView(); 

Krok 5: Dodaj kod, aby wywołać selektor wyboru pliku w przypadku zdarzenia Click Button.

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

Krok 6: Zdarzenie włączone w widoku Ustaw obraz na Stronę główną.

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

Umożliwia także zrobienie zdjęcia i korzystanie z niego.

referencyjny: Using FileOpenPicker in Windows Phone 8.1 to choose picture from Picture Gallery

+0

Nigdy nie używałeś zmiennej ImagePath wewnątrz kodu. Po co to jest? –

+0

Masz rację! Nigdy go nie używałem, nawet nie wiem, dlaczego tam jest, patrzę na moją obecną implementację, a ImagePath nie ma. W tej chwili edytuję swoją odpowiedź. Dzięki! – isscroberto

+0

, gdyby procedura obsługi zdarzeń nie została usunięta przed sprawdzeniem liczby, na wypadek gdyby użytkownik przerwał selektor plików bez zaznaczenia? – dlatikay

Powiązane problemy