次の方法で共有


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

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

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.

Task Method to call from a Windows Store app Method 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.

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

 

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.

 

Instructions

To call a FileOpenPicker and continue your app

  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, download the File picker sample, or see How to continue your Windows Phone app after calling an AndContinue method.

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

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

            protected async override void OnActivated(IActivatedEventArgs e)
            {
                ...
                ContinuationManager = new ContinuationManager();
                ...
            }
    
  5. Assume that one page of your app contains code that calls a FileOpenPicker to pick an existing file. 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 Scenario1 : Page, IFileOpenPickerContinuable
        {
            ...
        }
    
  6. Assume that the same 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 values for several properties.
    2. Finally it launches the FileOpenPicker by calling the PickSingleFileAndContinue method. Your app is deactivated until the user picks a file.
            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();
            }
    
  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.

  8. The user picks a file in the FileOpenPicker. Then your app is reactivated. In the app.xaml.cs file, in the OnActivated event handler, 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)
            {
                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();
            }
    
  9. The switch statement in the ContinuationManager helper class now calls the ContinueFileOpenPicker method defined by the IFileOpenPickerContinuable interface.

            switch (args.Kind)
            {
                case ActivationKind.PickFileContinuation:
                    var fileOpenPickerPage = rootFrame.Content as IFileOpenPickerContinuable;
                    if (fileOpenPickerPage != null)
                    {
                        fileOpenPickerPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs);
                    }
                    break;
    
                ...
            }
    
  10. You provide your own implementation for the ContinueFileOpenPicker method in the same page of your app that called the FileOpenPicker.

    Now the app can finish what it was doing before it called the FileOpenPicker. In this case, it it simply displays the name of the selected file in a TextBlock control.

            public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
            {
                if (args.Files.Count > 0)
                {
                    OutputTextBlock.Text = "Picked photo: " + args.Files[0].Name;
                }
                else
                {
                    OutputTextBlock.Text = "Operation cancelled.";
                }
            }
    

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

File picker sample