FileOpenPicker 不提供文件路径

FileOpenPicker not giving file path

我试图让 fileopenpicker 给我一个选定文件的文件路径,但是,当 fileopenpicker 恢复时,它没有给我文件路径,没有使用 ContinueFileOpenPicker 方法,我放置了一个断点在上面,它没有触发。关于这是为什么的任何想法? 我正在使用 Windows Phone Silverlight 8.1.

private async void btnPickFile_Click(object sender, RoutedEventArgs e)
    {
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.List;
        openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        openPicker.FileTypeFilter.Add("*");
        openPicker.ContinuationData["filePath"] = "GetFilePath";
        openPicker.PickSingleFileAndContinue();

    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var app = App.Current as App;
        if (app.FilePickerContinuationArgs != null)
        {
            this.ContinueFileOpenPicker(app.FilePickerContinuationArgs);
        }
    }

    public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
    {
        if ((args.ContinuationData["filePath"] as string) == "GetfilePath" &&
            args.Files != null &&
            args.Files.Count > 0)
        {
            StorageFile file = args.Files[0];
            string filePath = file.Path;
            FilePath.Text = file.Path;
        }         
    }

干杯:)

如果问题只是断点未命中,请确保您 运行在调试模式下安装应用程序。如果您 运行 您的应用处于发布模式,则不会命中任何断点。不过,如果您遇到问题,请继续阅读。

您可以按照 here 中的步骤操作。

您的代码看起来不错,但您可能在某处遗漏了一些东西。在这里,我用代码片段简要介绍了该过程。

第一步: 编写代码以在任何事件处理程序的特定页面中打开文件选择器。

    // MainPage.xaml.cs file

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.FileTypeFilter.Add("*");
        openPicker.CommitButtonText = "Select";
        openPicker.ContinuationData["Operation"] = "pickFile";
        openPicker.PickSingleFileAndContinue();
    }

那么在您项目的 App.xaml.cs 文件中添加的内容很少

第二步:App.xaml.cs 文件中为 FileOpenPickerContinuationEventArgs.

声明一个全局变量
    public FileOpenPickerContinuationEventArgs FilePickerContinuationArgs { get; set; }

第三步: 当您的应用程序在拾取文件后恢复时,它将调用 ContractActivated 事件,因此需要在 App.xaml.cs.

中的 InitializePhoneApplication 方法中处理该事件
    // Handle contract activation such as a file open or save picker
    PhoneApplicationService.Current.ContractActivated += Application_ContractActivated;

现在在 App.xaml.cs 文件中定义事件处理程序 Application_ContractActivated,如下所示。

    // App.xaml.cs file

    // Code to execute when a contract activation such as a file open or save picker returns 
    // with the picked file or other return values
    private void Application_ContractActivated(object sender, Windows.ApplicationModel.Activation.IActivatedEventArgs e)
    {
        var filePickerContinuationArgs = e as FileOpenPickerContinuationEventArgs;
        if (filePickerContinuationArgs != null)
        {
            this.FilePickerContinuationArgs = filePickerContinuationArgs;
        }
    }

第四步: 现在回到您要调用 FileOpenPicker 的页面并按以下方式覆盖 OnNavigatedTo() 方法。

    // MainPage.xaml.cs file

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var app = App.Current as App;
        if (app.FilePickerContinuationArgs != null)
        {
            this.ContinueFileOpenPicker(app.FilePickerContinuationArgs);
        }
    }

然后定义 ContinueFileOpenPicker 事件处理程序如下。

    // MainPage.xaml.cs file

    public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
    {
        if ((args.ContinuationData["Operation"] as string) == "pickFile" &&
            args.Files != null &&
            args.Files.Count > 0)
        {
            StorageFile file = args.Files[0];
            string filePath = file.Path;
            MessageBox.Show(filePath);
        }
    }

大功告成..!! 如果你做的一切正确,这段代码就可以工作。

希望对您有所帮助.. 干杯..

我认识到你真正的错误。

查看截图。

希望您也认识到自己的问题。

一个大写-小写字母的错误可以让开发者抓狂..! :-)