private async void AddFileButton_Click(object sender, RoutedEventArgs e) { StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(@"CachedFile.txt", CreationCollisionOption.ReplaceExisting); await Windows.Storage.FileIO.WriteTextAsync(file, @"Cached file created..."); CachedFileUpdater.SetUpdateInformation(file, "CachedFile", ReadActivationMode.BeforeAccess, WriteActivationMode.NotNeeded, CachedFileOptions.RequireUpdateOnAccess); bool inBasket; switch (fileOpenPickerUI.AddFile(id, file)) { case AddFileResult.Added: case AddFileResult.AlreadyAdded: inBasket = true; OutputTextBlock.Text = Status.FileAdded; break; default: inBasket = false; OutputTextBlock.Text = Status.FileAddFailed; break; } UpdateButtonState(inBasket); }
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(); }
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(); }
private async void RenameButton_Click(object sender, RoutedEventArgs e) { FileUpdateRequest fileUpdateRequest = CachedFileUpdaterPage.Current.fileUpdateRequest; FileUpdateRequestDeferral fileUpdateRequestDeferral = CachedFileUpdaterPage.Current.fileUpdateRequestDeferral; StorageFile file = await fileUpdateRequest.File.CopyAsync(ApplicationData.Current.LocalFolder, fileUpdateRequest.File.Name, NameCollisionOption.GenerateUniqueName); CachedFileUpdater.SetUpdateInformation(file, "CachedFile", ReadActivationMode.NotNeeded, WriteActivationMode.AfterWrite, CachedFileOptions.RequireUpdateOnAccess); fileUpdateRequest.UpdateLocalFile(file); this.OutputFileAsync(file); fileUpdateRequest.Status = FileUpdateStatus.CompleteAndRenamed; fileUpdateRequestDeferral.Complete(); UpdateUI(CachedFileUpdaterPage.Current.cachedFileUpdaterUI.UIStatus); }
// 本 CachedFile 用于从 Remote 更新(由 app 更新 CachedFile) private async void btnPickCachedFileRemote_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(@"webabcdCachedFileUpdaterRemote.txt", CreationCollisionOption.ReplaceExisting); string textContent = "I am webabcd"; await FileIO.WriteTextAsync(file, textContent); /* * 设置 CachedFile,即将文件关联到 CachedFileUpdater * SetUpdateInformation(IStorageFile file, string contentId, ReadActivationMode readMode, WriteActivationMode writeMode, CachedFileOptions options); * file - 与 CachedFileUpdater 关联的文件 * contentId - 与 CachedFileUpdater 关联的文件标识 */ CachedFileUpdater.SetUpdateInformation(file, "cachedFileRemote", ReadActivationMode.NotNeeded, WriteActivationMode.AfterWrite, CachedFileOptions.RequireUpdateOnAccess); lblMsg.Text = "选择的文件: " + file.Name; AddFileResult result = _fileOpenPickerUI.AddFile("myFile", file); }