2015-09-04 45 views
0

我需要製作樂器,它允許用戶從Gallery中選擇照片。選擇照片後,它將在ImageBox中顯示給用戶。WP8.1應用中的Filepicker沒有顯示任何照片

問題是,當用戶在Gallery中選擇一些照片時,Gallery關閉,ImageBox保持空白。代碼沒有返回錯誤。請幫我找到錯誤並解決這個問題。謝謝。

下面是一個代碼:

ImagePath = String.Empty 

    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() 


     Dim BitmapImage = New BitmapImage() 
     Await BitmapImage.SetSourceAsync(filePicker) 
     MyPhoto.Source = BitmapImage 

回答

0

如果您正在使用filePicker.PickSingleFileAndContinue()你需要添加代碼到App_Activated

你會發現,PickSingleFileAndContinue()是一種無效的方法將不會返回拿起文件分別爲PickSingleFileAsync()將返回一個文件。

代碼塊爲C#,對不起,我有vb只有有限的知識,您可以在下面

Similar SO Answer

C#

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(); 

當您正在使用PickSingleFileAndContinue()你找到VB範例需要執行ContinuationManager這是你得到你選擇的文件。

在App.xaml.cs

public ContinuationManager ContinuationManager { get; private set; } 

這將觸發時,應用程序得到激活

protected async override void OnActivated(IActivatedEventArgs e) 
    { 
     base.OnActivated(e); 

     continuationManager = new ContinuationManager(); 

     Frame rootFrame = CreateRootFrame(); 
     await RestoreStatusAsync(e.PreviousExecutionState); 

     if(rootFrame.Content == null) 
     { 
      rootFrame.Navigate(typeof(MainPage)); 
     } 

     var continuationEventArgs = e as IContinuationActivatedEventArgs; 
     if (continuationEventArgs != null) 
     { 
      Frame scenarioFrame = MainPage.Current.FindName("ScenarioFrame") as Frame; 
      if (scenarioFrame != null) 
      { 
       // Call ContinuationManager to handle continuation activation 
       continuationManager.Continue(continuationEventArgs, scenarioFrame); 
      } 
     } 

     Window.Current.Activate(); 
    } 

使用頁面

假設的那一頁您的應用程序包含調用FileOpenPicker來選擇現有文件的代碼。在這個類中,從ContinuationManager輔助類實現相應的接口。當您的應用程序使用FileOpenPicker時,要實現的接口是IFileOpenPickerContinuable。

public sealed partial class Scenario1 : Page, IFileOpenPickerContinuable 
{ 
    ... 

//inside this you have this 

private void PickAFileButton_Click(object sender, RoutedEventArgs 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(); 
    } 
} 


switch (args.Kind) 
    { 
     case ActivationKind.PickFileContinuation: 
      var fileOpenPickerPage = rootFrame.Content as IFileOpenPickerContinuable; 
      if (fileOpenPickerPage != null) 
      { 
       fileOpenPickerPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs); 
      } 
      break; 

     ... 
    } 

Download msdn sample here

Msdn documentation using FilePicker

+0

非常感謝您爲完整的答案 –