user3279195
user3279195

Reputation: 31

FileOpenPicker not giving file path

I'm trying to get a fileopenpicker to give me the filepath of a selected file, however, when the fileopenpicker resumes, it does not give me the file path, the ContinueFileOpenPicker method is not used, I placed a breakpoint on it, and it did not trigger. Any ideas as to why this is? I'm using using 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;
        }         
    }

Cheers :)

Upvotes: 1

Views: 668

Answers (2)

Keval Langalia
Keval Langalia

Reputation: 1892

I recognized your actual mistake here.

Check the screenshot.

enter image description here

I hope you also recognized your issue.

One mistake of Capital-Small letter can make a developer go mad..! :-)

Upvotes: 1

Keval Langalia
Keval Langalia

Reputation: 1892

If the issue is only of breakpoint do not hit, then make sure you're running the app in Debug mode. if you'll run your app in Release mode, then no breakpoint will hit. Still if you're facing issue, then read further.

You can follow the steps from here.

Your code looks okay but you might be missing something somewhere. here I'm briefing the procedure with code snippet.

First Step: write code to open file picker in the specific page in any event handler.

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

Then there are few things to be added in your project's App.xaml.cs file

Second Step: Declare a global variable in App.xaml.cs file for FileOpenPickerContinuationEventArgs.

    public FileOpenPickerContinuationEventArgs FilePickerContinuationArgs { get; set; }

Third Step: When your app resumes after picking up a file, it will call ContractActivated event, so need to handle that event in InitializePhoneApplication method in App.xaml.cs.

    // Handle contract activation such as a file open or save picker
    PhoneApplicationService.Current.ContractActivated += Application_ContractActivated;

Now define the event handler Application_ContractActivated in App.xaml.cs file as per below.

    // 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;
        }
    }

Fourth Step: Now come back to the page where you want to call FileOpenPicker and override the OnNavigatedTo() method as per below.

    // MainPage.xaml.cs file

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

Then define the ContinueFileOpenPicker event handler as per below.

    // 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);
        }
    }

You're done..!! This code is working if you do everything right.

Hope it helps.. Cheers..

Upvotes: 1

Related Questions