WP8.1应用程序中的Filepicker没有显示任何照片

时间:2015-09-04 08:02:07

标签: vb.net windows-phone-8 gallery filepicker

我需要制作乐器,允许用户从Gallery中选择照片。选择照片后,它将在ImageBox中显示给用户。

问题是,当用户在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

1 个答案:

答案 0 :(得分:0)

如果您使用的是filePicker.PickSingleFileAndContinue(),则需要在App_Activated

中添加代码

你会注意到PickSingleFileAndContinue()是一个void方法,不会返回被挑选的文件,因为PickSingleFileAsync()将返回一个文件。

代码块在C#,对不起我对vb的知识有限,你可以在下面找到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()时,您需要实施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