以保存文件为例
首先,在项目中加入ContinuationManager.cs类,以及SuspensionManager.cs类。
其次,在App.xaml.cs中,完成如下步骤:
1. 添加ContinuationManager类的实例作为属性。
public ContinuationManager ContinuationManager { get; private set; }
2. 加入如下的方法
// for continuable private Frame CreateRootFrame() { Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); // Set the default language rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0]; rootFrame.NavigationFailed += OnNavigationFailed; // Place the frame in the current Window Window.Current.Content = rootFrame; } return rootFrame; } private async Task RestoreStatusAsync(ApplicationExecutionState previousExecutionState) { // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (previousExecutionState == ApplicationExecutionState.Terminated) { // Restore the saved session state only when appropriate try { await SuspensionManager.RestoreAsync(); } catch (SuspensionManagerException) { //Something went wrong restoring state. //Assume there is no state and continue } } } void OnNavigationFailed(object sender, NavigationFailedEventArgs e) { throw new Exception("Failed to load Page " + e.SourcePageType.FullName); } 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) { // Call ContinuationManager to handle continuation activation ContinuationManager.Continue(continuationEventArgs); } Window.Current.Activate(); }
最后, 实现保存文件的操作, 在保存文件的页面,使该页面可以实现 IFileSavePickerContinuable接口。
public sealed partial class MainPage: Page, IFileSavePickerContinuable { string OriginalPictureUrl = string.Empty; private void DownloadAppBarButton_Click(object sender, RoutedEventArgs e) { var item = FlipViewControl.SelectedItem as ViewSource; if (null != item) { FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; savePicker.FileTypeChoices.Add("JPG File", new ObservableCollection<string>() { ".jpg" }); savePicker.DefaultFileExtension = ".jpg"; savePicker.SuggestedFileName = item.ViewImageUrl.Substring(item.ViewImageUrl.LastIndexOf("/") + 1); savePicker.PickSaveFileAndContinue(); OriginalPictureUrl = item.ViewImageUrl; } } public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args) { if (null != args.File) { StorageFile saveFile = args.File; var uri = new Uri(OriginalPictureUrl); var fileName = saveFile.DisplayName; var thumbnail = RandomAccessStreamReference.CreateFromUri(uri); var remoteFile = await StorageFile.CreateStreamedFileFromUriAsync(fileName, uri, thumbnail); await remoteFile.CopyAndReplaceAsync(saveFile); var toast = new ToastPrompt { Message = "download completed." }; toast.Show(); } } }
另外,若要实现打开文件的操作,页面需要实现 IFileOpenPickerContinuable 接口。
public sealed partial class SettingPage : Page, IFileOpenPickerContinuable { private void PickPhoto() { FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); openPicker.PickSingleFileAndContinue(); } public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args) { if (args.Files.Count > 0) { StorageFile bgFile = null; string id = Guid.NewGuid().ToString(); string bgFileName = string.Format(@"{0}.jpg", id); bgFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(bgFileName, CreationCollisionOption.OpenIfExists); App.BgImg = bgFileName; bool isSaved = await new data().SetBackground(App.BgImg); App.isBGChanged = true; await args.Files[0].CopyAndReplaceAsync(bgFile); // display the selected image ShowImage(); } } }