private async void OnTargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs e) { // This scenario demonstrates how the app can go about handling the TargetFileRequested event on the UI thread, from // which the app can manipulate the UI, show error dialogs, etc. // Requesting a deferral allows the app to return from this event handler and complete the request at a later time. // In this case, the deferral is required as the app intends on handling the TargetFileRequested event on the UI thread. // Note that the deferral can be requested more than once but calling Complete on the deferral a single time will complete // original TargetFileRequested event. var deferral = e.Request.GetDeferral(); await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { // This method will be called on the app's UI thread, which allows for actions like manipulating // the UI or showing error dialogs // Display a dialog indicating to the user that a corrective action needs to occur var errorDialog = new Windows.UI.Popups.MessageDialog("If the app needs the user to correct a problem before the app can save the file, the app can use a message like this to tell the user about the problem and how to correct it."); await errorDialog.ShowAsync(); // Set the targetFile property to null and complete the deferral to indicate failure once the user has closed the // dialog. This will allow the user to take any neccessary corrective action and click the Save button once again. e.Request.TargetFile = null; deferral.Complete(); }); }
internal void Activate(FileSavePickerActivatedEventArgs args) { fileSavePickerUI = args.FileSavePickerUI; Window.Current.Content = this; this.OnNavigatedTo(null); Window.Current.Activate(); }
public void Activate(FileSavePickerActivatedEventArgs args) { this.fileSavePickerUI = args.FileSavePickerUI; this.fileSavePickerUI.TargetFileRequested += fileSavePickerUI_TargetFileRequested; Window.Current.Content = this; Window.Current.Activate(); }
protected override void OnNavigatedTo(NavigationEventArgs e) { // 获取 FileSavePickerUI 对象(从 App.xaml.cs 传来的) FileSavePickerActivatedEventArgs args = (FileSavePickerActivatedEventArgs)e.Parameter; _fileSavePickerUI = args.FileSavePickerUI; _fileSavePickerUI.Title = "自定义文件保存选取器"; // 通过 AllowedFileTypes 获取到的允许的扩展名是在调用端的 FileSavePicker.FileTypeChoices 中配置的,实际保存扩展名可以不与其匹配 IReadOnlyList <string> allowedFileTypes = _fileSavePickerUI.AllowedFileTypes; lblMsg.Text = "allowedFileTypes: " + string.Join(",", allowedFileTypes); lblMsg.Text += Environment.NewLine; lblMsg.Text += "Kind: " + args.Kind; lblMsg.Text += Environment.NewLine; lblMsg.Text += "SplashScreen.ImageLocation: " + args.SplashScreen.ImageLocation; lblMsg.Text += Environment.NewLine; lblMsg.Text += "PreviousExecutionState: " + args.PreviousExecutionState; lblMsg.Text += Environment.NewLine; lblMsg.Text += "CallerPackageFamilyName: " + args.CallerPackageFamilyName; lblMsg.Text += Environment.NewLine; lblMsg.Text += "User.NonRoamableId: " + args.User.NonRoamableId; lblMsg.Text += Environment.NewLine; _fileSavePickerUI.TargetFileRequested += _fileSavePickerUI_TargetFileRequested; }
private async void OnTargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs args) { var deferral = args.Request.GetDeferral(); await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { // This method will be called on the app's UI thread, which allows for actions like manipulating // the UI or showing error dialogs try { var folder = await StorageFolder.GetFolderFromPathAsync(SavePath); var file = await folder.CreateFileAsync(sender.FileName, CreationCollisionOption.GenerateUniqueName); args.Request.TargetFile = file; deferral.Complete(); } catch (ArgumentException) { // originates from folder access var errorDialog = new MessageDialog( "The save picker app you chose gave an invalid folder path. Please check again."); await errorDialog.ShowAsync(); args.Request.TargetFile = null; deferral.Complete(); } catch (UnauthorizedAccessException) { // can originate from folder access or file creation var errorDialog = new MessageDialog( "You do not have permission to save a file in this folder. Please check your app permissions."); await errorDialog.ShowAsync(); args.Request.TargetFile = null; deferral.Complete(); } catch (FileNotFoundException) { // either folder does not exist or file name is invalid var errorDialog = new MessageDialog( "The file name may contain invalid characters. Please check your file name again."); await errorDialog.ShowAsync(); args.Request.TargetFile = null; deferral.Complete(); } catch (Exception ex) { var errorDialog = new MessageDialog( $"An error has occurred: {ex.Message}"); await errorDialog.ShowAsync(); args.Request.TargetFile = null; deferral.Complete(); } finally { deferral.Complete(); } }); }
private async void OnTargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs e) { // This scenario demonstrates how to handle the TargetFileRequested event on the background thread on which it was raised // Requesting a deferral allows the app to call another asynchronous method and complete the request at a later time var deferral = e.Request.GetDeferral(); StorageFile file; // If the checkbox is checked then the requested file name will be ConflictingFile.txt instead of what was sent to us in sender.name. // If background task sees that ConflictingFile is in the name of the file it sets the returned status to FileUpdateStatus.UserInputNeeded. // This will cause a prompt for the user to open the app to fix the conflict. if (simulateUpdateConflict) { file = await ApplicationData.Current.LocalFolder.CreateFileAsync("ConflictingFile.txt", CreationCollisionOption.ReplaceExisting); } else { file = await ApplicationData.Current.LocalFolder.CreateFileAsync(sender.FileName, CreationCollisionOption.ReplaceExisting); } CachedFileUpdater.SetUpdateInformation(file, "CachedFile", ReadActivationMode.NotNeeded, WriteActivationMode.AfterWrite, CachedFileOptions.RequireUpdateOnAccess); e.Request.TargetFile = file; // Complete the deferral to let the Picker know the request is finished deferral.Complete(); }
async void _save_basket_TargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs args) { var deferral = args.Request.GetDeferral(); args.Request.TargetFile = await ApplicationData.Current.RoamingFolder.CreateFileAsync($"resumes\\{_save_basket.FileName}", CreationCollisionOption.GenerateUniqueName); deferral.Complete(); }
public void Activate(FileSavePickerActivatedEventArgs args) { _saving = true; _save_basket = args.FileSavePickerUI; _save_basket.TargetFileRequested += _save_basket_TargetFileRequested; Window.Current.Content = this; Window.Current.Activate(); }
private async void OnTargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs e) { // This scenario demonstrates how to handle the TargetFileRequested event on the background thread on which it was raised // Requesting a deferral allows the app to call another asynchronous method and complete the request at a later time var deferral = e.Request.GetDeferral(); e.Request.TargetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(sender.FileName, CreationCollisionOption.GenerateUniqueName); // Complete the deferral to let the Picker know the request is finished deferral.Complete(); }
private async void OnTargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs e) { // This scenario demonstrates how to handle the TargetFileRequested event on the background thread on which it was raised // Requesting a deferral allows the app to call another asynchronous method and complete the request at a later time var deferral = e.Request.GetDeferral(); StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(sender.FileName, CreationCollisionOption.ReplaceExisting); CachedFileUpdater.SetUpdateInformation(file, "CachedFile", ReadActivationMode.NotNeeded, WriteActivationMode.AfterWrite, CachedFileOptions.RequireUpdateOnAccess); e.Request.TargetFile = file; // Complete the deferral to let the Picker know the request is finished deferral.Complete(); }
/// <summary> /// File Save Operation, create new file under Images folder and linked the new file to the selected note. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> async void fileSavePickerUI_TargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs args) { if (note == null) { Helpers.ShowMessageAsync("Please select at least one Note to save the image.", "Save Image"); return; } var deferral = args.Request.GetDeferral(); args.Request.TargetFile = await DataManager.CreateFileAsync(this.fileSavePickerUI.FileName, FolderNames.Images); note.Images.Add(string.Format("{0}/{1}", FolderNames.Images, args.Request.TargetFile.Name)); DataManager.Save(); // Complete the deferral to let the Picker know the request is finished deferral.Complete(); }
/// <summary> /// Used to set up the content to be displayed and connect to the containing File Open Picker /// </summary> /// <param name="fileSavePickerUI">The file save picker UI element.</param> public void Initialize(FileSavePickerUI fileSavePickerUI) { if (fileSavePickerUI == null) { throw new ArgumentNullException("fileSavePickerUI"); } // Tie into the containing File Picker object _fileSavePickerUI = fileSavePickerUI; _fileSavePickerUI.TargetFileRequested += HandleTargetFileRequested; // Initialize the ViewModel DefaultViewModel["CanGoUp"] = false; DefaultViewModel["Contacts"] = null; DefaultViewModel["SelectedContact"] = null; // Load the contact info LoadContacts(); }
/// <summary> /// Invoked when the user presses the "Save" button in the Picker /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The <see cref="TargetFileRequestedEventArgs"/> instance containing the event data.</param> private async void HandleTargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs args) { var currentContact = DefaultViewModel["SelectedContact"] as Contact; if (currentContact == null) { return; } // Requesting a deferral allows the app to call an // asynchronous method and complete the request. var deferral = args.Request.GetDeferral(); // Get the target file based on the currently selected contact args.Request.TargetFile = await currentContact.SaveRelatedFile(sender.FileName); // Complete the deferral to let the Picker know // that the request processing has finished deferral.Complete(); }
private async void _fileSavePickerUI_TargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs args) { // 异步操作 var deferral = args.Request.GetDeferral(); try { // 在指定的地址新建一个没有任何内容的空白文件 StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(sender.FileName, CreationCollisionOption.GenerateUniqueName); // 设置 TargetFile,“自定义文件保存选取器”的调用端会收到此对象 args.Request.TargetFile = file; } catch (Exception ex) { // 输出异常信息 OutputMessage(ex.ToString()); } finally { // 完成异步操作 deferral.Complete(); } }
public FileSavePayload(FileSavePickerUI fileSavePickerUI) { this.fileSavePickerUI = fileSavePickerUI; FileTypes = fileSavePickerUI.AllowedFileTypes; this.fileSavePickerUI.TargetFileRequested += OnTargetFileRequested; }