所以我现在告诉 FileSavePicker 只创建一个空白文件,并且我必须编写额外的代码才能真正写入文件。我在 FileSavePicker 之后启动了一个任务 WriteToFile,但我不确定如何完成它。使用 FileSavePicker,用户可以选择他们希望将文件保存到的文件夹。在 WriteToFile 代码中我应该在哪里指出它,以及我究竟如何将文件源放入其中?要保存的文件都与应用程序打包在一起。我在这里以 x.mp3 为例。
public class SoundData : ViewModelBase
{
public string Title { get; set; }
public string FilePath { get; set; }
public RelayCommand<string> SaveSoundAs { get; set; }
private async void ExecuteSaveSoundAs(string soundPath)
{
string path = @"appdata:/x.mp3";
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await folder.GetFileAsync(path);
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedSaveFile = file;
savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
savePicker.ContinuationData.Add("SourceSound", soundPath);
savePicker.SuggestedFileName = this.Title;
savePicker.PickSaveFileAndContinue();
}
}
public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
{
string soundPath = (string)args.ContinuationData["SourceSound"];
StorageFile file = args.File;
if (file != null)
{
// Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
CachedFileManager.DeferUpdates(file);
// write to file
await FileIO.WriteTextAsync(file, file.Name);
// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete) ;
}
}
public SoundData()
{
SaveSoundAs = new RelayCommand<string>(ExecuteSaveSoundAs);
}
}
}