using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.ApplicationModel.Activation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; #if WINDOWS_PHONE_APP /// /// ContinuationManager is used to detect if the most recent activation was due /// to a continuation such as the FileOpenPicker or WebAuthenticationBroker /// public class ContinuationManager { IContinuationActivatedEventArgs args = null; bool handled = false; Guid id = Guid.Empty; /// /// Sets the ContinuationArgs for this instance. Using default Frame of current Window /// Should be called by the main activation handling code in App.xaml.cs /// /// The activation args internal void Continue(IContinuationActivatedEventArgs args) { Continue(args, Window.Current.Content as Frame); } /// /// Sets the ContinuationArgs for this instance. Should be called by the main activation /// handling code in App.xaml.cs /// /// The activation args /// The frame control that contains the current page internal void Continue(IContinuationActivatedEventArgs args, Frame rootFrame) { if (args == null) throw new ArgumentNullException("args"); if (this.args != null && !handled) throw new InvalidOperationException("Can't set args more than once"); this.args = args; this.handled = false; this.id = Guid.NewGuid(); if (rootFrame == null) return; switch (args.Kind) { case ActivationKind.PickFileContinuation: var fileOpenPickerPage = rootFrame.Content as IFileOpenPickerContinuable; if (fileOpenPickerPage != null) { fileOpenPickerPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs); } break; case ActivationKind.PickSaveFileContinuation: var fileSavePickerPage = rootFrame.Content as IFileSavePickerContinuable; if (fileSavePickerPage != null) { fileSavePickerPage.ContinueFileSavePicker(args as FileSavePickerContinuationEventArgs); } break; case ActivationKind.PickFolderContinuation: var folderPickerPage = rootFrame.Content as IFolderPickerContinuable; if (folderPickerPage != null) { folderPickerPage.ContinueFolderPicker(args as FolderPickerContinuationEventArgs); } break; case ActivationKind.WebAuthenticationBrokerContinuation: var wabPage = rootFrame.Content as IWebAuthenticationContinuable; if (wabPage != null) { wabPage.ContinueWebAuthentication(args as WebAuthenticationBrokerContinuationEventArgs); } break; } } /// /// Marks the contination data as 'stale', meaning that it is probably no longer of /// any use. Called when the app is suspended (to ensure future activations don't appear /// to be for the same continuation) and whenever the continuation data is retrieved /// (so that it isn't retrieved on subsequent navigations) /// internal void MarkAsStale() { this.handled = true; } /// /// Retrieves the continuation args, if they have not already been retrieved, and /// prevents further retrieval via this property (to avoid accidentla double-usage) /// public IContinuationActivatedEventArgs ContinuationArgs { get { if (handled) return null; MarkAsStale(); return args; } } /// /// Unique identifier for this particular continuation. Most useful for components that /// retrieve the continuation data via and need /// to perform their own replay check /// public Guid Id { get { return id; } } /// /// Retrieves the continuation args, optionally retrieving them even if they have already /// been retrieved /// /// Set to true to return args even if they have previously been returned /// The continuation args, or null if there aren't any public IContinuationActivatedEventArgs GetContinuationArgs(bool includeStaleArgs) { if (!includeStaleArgs && handled) return null; MarkAsStale(); return args; } } /// /// Implement this interface if your page invokes the file open picker /// API. /// interface IFileOpenPickerContinuable { /// /// This method is invoked when the file open picker returns picked /// files /// /// Activated event args object that contains returned files from file open picker void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args); } /// /// Implement this interface if your page invokes the file save picker /// API /// interface IFileSavePickerContinuable { /// /// This method is invoked when the file save picker returns saved /// files /// /// Activated event args object that contains returned file from file save picker void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args); } /// /// Implement this interface if your page invokes the folder picker API /// interface IFolderPickerContinuable { /// /// This method is invoked when the folder picker returns the picked /// folder /// /// Activated event args object that contains returned folder from folder picker void ContinueFolderPicker(FolderPickerContinuationEventArgs args); } /// /// Implement this interface if your page invokes the web authentication /// broker /// interface IWebAuthenticationContinuable { /// /// This method is invoked when the web authentication broker returns /// with the authentication result /// /// Activated event args object that contains returned authentication token void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args); } #endif