Continue your Windows Phone Silverlight 8.1 app after calling a file picker

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

When you call a file picker from a Windows Phone app, your app is deactivated until the file picker returns the selection made by the user. On low-memory phones, however, your app may be terminated. Because of this possibility, you have to call different methods in a Windows Phone app than you call in a Windows Store app to continue your app after these operations. The following table shows these methods.

Task

Method to call from a Windows Store app

Method to call from a Windows Phone app

Pick a file to open

PickSingleFileAsync()()()

PickSingleFileAndContinue

Pick a location and filename to save a file

PickSaveFileAsync

PickSaveFileAndContinue

Pick a folder

PickSingleFolderAsync

PickFolderAndContinue

The example in this topic demonstrates how to continue your Windows Phone Silverlight 8.1 app when you use a FileOpenPicker. Use similar code when you call other file and folder picker methods.

Continuing your app after calling a file picker

The following example assumes that the user is selecting a new photo for her profile by using a FileOpenPicker.

To call a FileOpenPicker and continue your app

  1. Assume that the Profile page of your app contains the following code to call the FileOpenPicker. This code does the following things:

            private void ChangePicture_Click(object sender, RoutedEventArgs e)
            {
                var picker = new FileOpenPicker();
                picker.FileTypeFilter.Add(".jpg");
                picker.ContinuationData["Operation"] = "UpdateProfilePicture";
                picker.PickSingleFileAndContinue();
            }
    
  2. In the app.xaml.cs file, declare a class-level variable to hold the FileOpenPickerContinuationEventArgs. When your app is reactivated, these event args return the ContinuationData that you set before you called the FileOpenPicker.

            public FileOpenPickerContinuationEventArgs FilePickerContinuationArgs { get; set; }
    
  3. In the app.xaml.cs file, in the InitializePhoneApplication method, attach the ContractActivated event handler.

                // Handle contract activation such as returned values from file open or save picker
                PhoneApplicationService.Current.ContractActivated += Application_ContractActivated;
    
  4. The user picks a file in the FileOpenPicker. Then your app is reactivated. In the app.xaml.cs file, in the handler for the Application_ContractActivated event, check whether this activation is a continuation. If this activation is a continuation, capture the FileOpenPickerContinuationEventArgs in the application-level variable that you declared previously.

            // Code to execute when the application is activated (brought to foreground)
            // This code will not execute when the application is first launched
            private void Application_ContractActivated(object sender, IActivatedEventArgs e)
            {
                var filePickerContinuationArgs = e as FileOpenPickerContinuationEventArgs;
                if (filePickerContinuationArgs != null)
                {
                    this.FilePickerContinuationArgs = filePickerContinuationArgs;
                }
            }
    
  5. The navigation framework for Silverlight apps restores the navigation state and returns the user to the same page that called the FileOpenPicker.

    In the handler for the OnNavigatedTo event of the Profile page of your app, check again whether this activation is a continuation. If this activation is a continuation, call the user-defined ContinueFileOpenPicker method in the same page and pass the FileOpenPickerContinuationEventArgs.

            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                var app = App.Current as App;
                if (app.FilePickerContinuationArgs != null)
                {
                    this.ContinueFileOpenPicker(app.FilePickerContinuationArgs);
                }
            }
    
  6. Finally the ContinueFileOpenPicker method in the Profile page of your app checks the ContinuationData returned in the [FileOpenPickerContinuationEventArgs](https://go.microsoft.com/fwlink/p/?linkid=394865\\).

    Now the app can finish what it was doing before it called the FileOpenPicker. In this case, it finishes updating the user's profile picture with the image file that she picked.

            public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
            {
                if ((args.ContinuationData["Operation"] as string) == "UpdateProfilePicture" &&
                    args.Files != null &&
                    args.Files.Count > 0)
                {
                    StorageFile file = args.Files[0];
    
                    if (file.Name.EndsWith("jpg"))
                    {
                        IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                        BitmapImage bitmapImage = new BitmapImage();
    
                        bitmapImage.SetSource(fileStream.AsStream());
                        ProfilePic.Source = bitmapImage;
                    }
                }
            }