How to continue your Windows Phone app after calling a file picker (XAML)

Applies to Windows Phone only

When you call a file picker from a Windows Phone Store 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 Store app than you call in a Windows Store app to continue your app after a file picker operation. The following table shows these methods.

TaskMethod to call from a Windows Store appMethod to call from a Windows Phone Store 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 app when you use a FileOpenPicker. Use similar code when you call other file and folder picker methods.

Important  For more info about this solution and about the helper classes it uses, see How to continue your Windows Phone app after calling an AndContinue method. The linked topic also describes how to debug your app after it calls the FileOpenPicker and then continues.

Tip  To see an example of this solution, see the File picker sample.

Instructions

To call a FileOpenPicker and continue your app

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

  1. Include the SuspensionManager helper class in your project. This class simplifies lifecycle management for your app. To get the SuspensionManager helper class, create a new Windows Phone app that uses a project template other than the Blank App template. The SuspensionManager.cs file is in the Common folder of the project.
  2. Include a helper class like the custom ContinuationManager helper class in your project. This class includes interfaces and methods that make it easier to continue your app. To get the code for the ContinuationManager helper class, see How to continue your Windows Phone app after calling an AndContinue method.

  3. In the app.xaml.cs file, declare a static instance of the ContinuationManager helper class at the class level.

    
            public static ContinuationManager ContinuationManager { get; private set; }
    
    
  4. In the app.xaml.cs file, instantiate the static instance of the ContinuationManager helper class in the class constructor.

    
            public App()
            {
                ...
                ContinuationManager = new ContinuationManager();
            }
    
    
    
    
  5. Assume that the Profile page of your app contains code that calls a FileOpenPicker to pick an existing photo. In this class, implement the corresponding interface from the ContinuationManager helper class. When your app uses a FileOpenPicker, the interface to implement is IFileOpenPickerContinuable.

    
        public sealed partial class ProfilePage : Page, IFileOpenPickerContinuable
        {
            ...
        }
    
    
    
  6. Assume that the Profile page of your app also contains the following code to call the FileOpenPicker. This code does the following things:

    1. After instantiating the FileOpenPicker, it specifies the type of files to display.
    2. It saves the optional ContinuationData that you can use when your app continues to complete the operation. The optional ContinuationData is useful if your app calls the FileOpenPicker from multiple locations on the same page, and you have to be able to identify the conditions in which it was called.
    3. Finally it launches the FileOpenPicker by calling the PickSingleFileAndContinue method. Your app is deactivated until the user picks a file.
    
            private void ChangePicture_Click(object sender, RoutedEventArgs e)
            {
                var picker = new FileOpenPicker();
                picker.FileTypeFilter.Add(".jpg");
                picker.ContinuationData["Operation"] = "UpdateProfilePicture";
                picker.PickSingleFileAndContinue();
            }
    
    
    
  7. After your app calls the FileOpenPicker, the app is suspended. The handler for the Application.Suspending event in the app.xaml.cs file calls the SaveAsync method of the SuspensionManager helper class to save application state. Then it calls the ContinuationManager helper class to mark any existing continuation data as stale.

    
            private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                Debug.WriteLine("OnSuspending");
    
                var deferral = e.SuspendingOperation.GetDeferral();
                await SuspensionManager.SaveAsync();
                ContinuationManager.MarkAsStale();
                deferral.Complete();
            }
    
    
    
  8. 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.Activated event, restore application state by calling the RestoreAsync method of the SuspensionManager helper class. Then check whether this activation is a continuation. If this activation is a continuation, call the Continue method of the ContinuationManager helper class.

    
    protected async override void OnActivated(IActivatedEventArgs e)
            {
                Debug.WriteLine("OnActivated: " + e.PreviousExecutionState.ToString());
    
                CreateRootFrame();
    
                // Restore the saved session state only when appropriate
                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    try
                    {
                        await SuspensionManager.RestoreAsync();
                    }
                    catch (SuspensionManagerException)
                    {
                        //Something went wrong restoring state.
                        //Assume there is no state and continue
                    }
                }
    
                //Check if this is a continuation
                var continuationEventArgs = e as IContinuationActivatedEventArgs;
                if (continuationEventArgs != null)
                {
                    ContinuationManager.Continue(continuationEventArgs);
                }
    
                Window.Current.Activate();
            }
    
    
    
  9. The switch statement in the ContinuationManager helper class now calls the ContinueFileOpenPicker method defined by the IFileOpenPickerContinuable interface. You provide your own implementation for the ContinueFileOpenPicker method in the same Profile page of your app that called the FileOpenPicker.

    
                switch (args.Kind)
                {
                    case ActivationKind.PickFileContinuation:
                        var fileOpenPickerPage = rootFrame.Content as IFileOpenPickerContinuable;
                        if (fileOpenPickerPage != null)
                        {
                            fileOpenPickerPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs);
                        }
                        break;
    
                    ...
    
                }
    
    
    
  10. Finally the ContinueFileOpenPicker method checks the optional ContinuationData returned in the FileOpenPickerContinuationEventArgs. This is the final step in continuing the app.

    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.Count > 0)
                {
                    StorageFile file = args.Files[0];
                    IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                    BitmapImage bitmapImage = new BitmapImage();
    
                    bitmapImage.SetSource(fileStream);
                    ProfilePic.Source = bitmapImage;
                }
            }
    
    
    

Related topics

How to continue your Windows Phone app after calling an AndContinue method
File picker sample

 

 

Show:
© 2014 Microsoft