2013-03-12 58 views
1

我想隨便挑使用文件:OpenFilePicker(不支持指定的方法)

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

      StorageFile file = await openPicker.PickSingleFileAsync(); 
      if (file != null) 
      { 
       // Application now has read/write access to the picked file 
       txt.Text = "Picked file: " + file.Name; 
      } 
      else 
      { 
       txt.Text = "Operation cancelled."; 
      } 

    } 
    catch (Exception exception) 
    { 
     txt.Text = exception.Message; 
    } 
} 

...但它拋出一個異常:不支持'指定的方法。 「;

我從Windows Phone 8文檔複製並粘貼了代碼,他們的示例都沒有工作,我想也許我錯過了文檔功能/合同等等,但是他們甚至都不存在於VS for Phone應用程序

爲什麼不能正常工作?

我已經跟蹤它的嘗試的第一行:

FileOpenPicker openPicker = new FileOpenPicker(); // this is the line the exception is thrown on. 
+0

使用AndContinue()代替PickSingleFileAsync。有關如何使用它的詳細信息,請參閱我的答案。由於手機內存方面的考慮,windows Phone團隊需要像這樣實現它。同時打開文件選取器應用程序和您的應用程序是不可行的。相反,您的應用程序暫停,文件選取器打開,然後您的應用程序恢復。 – 2014-06-03 17:09:14

回答

5

按照MSDN論壇和從我假設的答案是MS的員工(here):

我們目前不支持選擇比照片或 選擇從其他商店的應用程序文件的其他文件。

所以它看起來像你堅持PhotoChooserTask而不是FileOpenPicker

+0

我打算簡單地投票,因爲我不同意他們決定不讓我們選擇其他文件類型。大聲笑。但我沒有。 +1 - 至少我知道,現在。 – James 2013-03-13 02:29:31

1

您只能使用FileOpenPicker從本地應用程序一樣的Direct3D的。

您可以使用PhotoChooserTask代替從圖片集線器中選擇圖片。

+0

什麼?!...那麼我們如何讓用戶選擇一個文件? : - /我們是否至少有某種目錄/文件查找器? – James 2013-03-12 09:41:58

+0

等等...但是,如果你需要使用C++和Direct3D,那麼爲什麼這個文檔甚至可以在C#中使用? – James 2013-03-12 09:42:48

+0

這些C#示例適用於Windows 8,不適用於WP8!我已經更新了我的回答 – 2013-03-12 09:56:57

5

這並不工作,但僅適用於Windows Phone的8.1(Windows手機)和不是Windows Phone 8.0/8.1(Windows Phone Silverlight)。

下面是代碼:

FileOpenPicker singleFilePicker = new FileOpenPicker(); 
     singleFilePicker.ViewMode = PickerViewMode.Thumbnail; 
     singleFilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
     singleFilePicker.FileTypeFilter.Add(".jpg"); 
     singleFilePicker.FileTypeFilter.Add(".jpeg"); 
     singleFilePicker.FileTypeFilter.Add(".png"); 
     singleFilePicker.PickSingleFileAndContinue(); 

添加此方法以處理所選擇的照片(S):

public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args) 
    { 
     if (args.Files.Count > 0) 
     { 
      var userChosenbPhoto = args.Files[0].Name; 
     } 
     else 
     { 
      //user canceled picker 
     } 
    } 

您也可以搶多個文件,太。

最後但也是最重要的一點,您需要爲您的項目添加一個延續管理器類。這將管理從拾取器返回時重新激活應用程序。 Go to this documentation to learn how to add a ContinuationManager該項目(抱歉的鏈接,太多的信息放在這裏)。

相關問題