ContinuationManager.cs (6800B)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Windows.ApplicationModel.Activation; 7 using Windows.UI.Xaml; 8 using Windows.UI.Xaml.Controls; 9 10 #if WINDOWS_PHONE_APP 11 /// <summary> 12 /// ContinuationManager is used to detect if the most recent activation was due 13 /// to a continuation such as the FileOpenPicker or WebAuthenticationBroker 14 /// </summary> 15 public class ContinuationManager 16 { 17 IContinuationActivatedEventArgs args = null; 18 bool handled = false; 19 Guid id = Guid.Empty; 20 21 /// <summary> 22 /// Sets the ContinuationArgs for this instance. Using default Frame of current Window 23 /// Should be called by the main activation handling code in App.xaml.cs 24 /// </summary> 25 /// <param name="args">The activation args</param> 26 internal void Continue(IContinuationActivatedEventArgs args) 27 { 28 Continue(args, Window.Current.Content as Frame); 29 } 30 31 /// <summary> 32 /// Sets the ContinuationArgs for this instance. Should be called by the main activation 33 /// handling code in App.xaml.cs 34 /// </summary> 35 /// <param name="args">The activation args</param> 36 /// <param name="rootFrame">The frame control that contains the current page</param> 37 internal void Continue(IContinuationActivatedEventArgs args, Frame rootFrame) 38 { 39 if (args == null) 40 throw new ArgumentNullException("args"); 41 42 if (this.args != null && !handled) 43 throw new InvalidOperationException("Can't set args more than once"); 44 45 this.args = args; 46 this.handled = false; 47 this.id = Guid.NewGuid(); 48 49 if (rootFrame == null) 50 return; 51 52 switch (args.Kind) 53 { 54 case ActivationKind.PickFileContinuation: 55 var fileOpenPickerPage = rootFrame.Content as IFileOpenPickerContinuable; 56 if (fileOpenPickerPage != null) 57 { 58 fileOpenPickerPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs); 59 } 60 break; 61 62 case ActivationKind.PickSaveFileContinuation: 63 var fileSavePickerPage = rootFrame.Content as IFileSavePickerContinuable; 64 if (fileSavePickerPage != null) 65 { 66 fileSavePickerPage.ContinueFileSavePicker(args as FileSavePickerContinuationEventArgs); 67 } 68 break; 69 70 case ActivationKind.PickFolderContinuation: 71 var folderPickerPage = rootFrame.Content as IFolderPickerContinuable; 72 if (folderPickerPage != null) 73 { 74 folderPickerPage.ContinueFolderPicker(args as FolderPickerContinuationEventArgs); 75 } 76 break; 77 78 case ActivationKind.WebAuthenticationBrokerContinuation: 79 var wabPage = rootFrame.Content as IWebAuthenticationContinuable; 80 if (wabPage != null) 81 { 82 wabPage.ContinueWebAuthentication(args as WebAuthenticationBrokerContinuationEventArgs); 83 } 84 break; 85 } 86 } 87 88 /// <summary> 89 /// Marks the contination data as 'stale', meaning that it is probably no longer of 90 /// any use. Called when the app is suspended (to ensure future activations don't appear 91 /// to be for the same continuation) and whenever the continuation data is retrieved 92 /// (so that it isn't retrieved on subsequent navigations) 93 /// </summary> 94 internal void MarkAsStale() 95 { 96 this.handled = true; 97 } 98 99 /// <summary> 100 /// Retrieves the continuation args, if they have not already been retrieved, and 101 /// prevents further retrieval via this property (to avoid accidentla double-usage) 102 /// </summary> 103 public IContinuationActivatedEventArgs ContinuationArgs 104 { 105 get 106 { 107 if (handled) 108 return null; 109 MarkAsStale(); 110 return args; 111 } 112 } 113 114 /// <summary> 115 /// Unique identifier for this particular continuation. Most useful for components that 116 /// retrieve the continuation data via <see cref="GetContinuationArgs"/> and need 117 /// to perform their own replay check 118 /// </summary> 119 public Guid Id { get { return id; } } 120 121 /// <summary> 122 /// Retrieves the continuation args, optionally retrieving them even if they have already 123 /// been retrieved 124 /// </summary> 125 /// <param name="includeStaleArgs">Set to true to return args even if they have previously been returned</param> 126 /// <returns>The continuation args, or null if there aren't any</returns> 127 public IContinuationActivatedEventArgs GetContinuationArgs(bool includeStaleArgs) 128 { 129 if (!includeStaleArgs && handled) 130 return null; 131 MarkAsStale(); 132 return args; 133 } 134 } 135 136 /// <summary> 137 /// Implement this interface if your page invokes the file open picker 138 /// API. 139 /// </summary> 140 interface IFileOpenPickerContinuable 141 { 142 /// <summary> 143 /// This method is invoked when the file open picker returns picked 144 /// files 145 /// </summary> 146 /// <param name="args">Activated event args object that contains returned files from file open picker</param> 147 void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args); 148 } 149 150 /// <summary> 151 /// Implement this interface if your page invokes the file save picker 152 /// API 153 /// </summary> 154 interface IFileSavePickerContinuable 155 { 156 /// <summary> 157 /// This method is invoked when the file save picker returns saved 158 /// files 159 /// </summary> 160 /// <param name="args">Activated event args object that contains returned file from file save picker</param> 161 void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args); 162 } 163 164 /// <summary> 165 /// Implement this interface if your page invokes the folder picker API 166 /// </summary> 167 interface IFolderPickerContinuable 168 { 169 /// <summary> 170 /// This method is invoked when the folder picker returns the picked 171 /// folder 172 /// </summary> 173 /// <param name="args">Activated event args object that contains returned folder from folder picker</param> 174 void ContinueFolderPicker(FolderPickerContinuationEventArgs args); 175 } 176 177 /// <summary> 178 /// Implement this interface if your page invokes the web authentication 179 /// broker 180 /// </summary> 181 interface IWebAuthenticationContinuable 182 { 183 /// <summary> 184 /// This method is invoked when the web authentication broker returns 185 /// with the authentication result 186 /// </summary> 187 /// <param name="args">Activated event args object that contains returned authentication token</param> 188 void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args); 189 } 190 191 #endif