【问题标题】:Filepicker in WP8.1 app did not show any photoWP8.1 应用程序中的文件选择器没有显示任何照片
【发布时间】:2015-09-04 08:02:06
【问题描述】:

我需要制作仪器,允许用户从图库中选择照片。选择照片后,会在 ImageBox 中显示给用户。

问题是,当用户在图库中选择一些照片时,图库关闭,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

【问题讨论】:

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


    【解决方案1】:

    如果您使用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

    【讨论】:

    • 非常感谢您的完整回答
    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多