因此,我现在被告知,FileSavePicker只创建一个空白文件,我必须编写额外的代码才能真正写入该文件。我已经在WriteToFile之后启动了一个任务FileSavePicker,但我不知道如何完成它。使用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);
}
}
}发布于 2014-06-04 22:43:50
对于Windows,您应该遵循这个如何文档化。与Silverlight应用程序相比,工作流已经发生了很大变化。你的应用程序不再像以前那样恢复旧任务了。
您不需要遵循文档中的所有步骤,但是一个重要的部分是在App.xaml.cs中遍历App.xaml.cs方法。在其中,您将调用您的ContinueFileSavePicker方法。
下面是您也可以下载的一个例子,这应该会有所帮助。
更新
如果要保存将随应用程序传送的文件,请尝试以下代码来初始化选择器
// Get the local file that is shipped with the app
// file but be "content" and not "resource"
string path = @"Assets\Audio\Sound.mp3";
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await folder.GetFileAsync(path);
// Show the picker
FileSavePicker savePicker = new FileSavePicker();
// Set the file that will be saved
savePicker.SuggestedSaveFile = file;
savePicker.SuggestedFileName = "Sound";
savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
savePicker.PickSaveFileAndContinue();您还需要确保侦听PhoneApplicationService的PhoneApplicationService事件。当应用程序从选择器返回时,这个事件就会被触发(在8.1 Silverlight应用程序中)。这就是您想要调用ContinueFileSavePicker方法的地方。如果你愿意的话,你可以把逻辑放进去。
订阅xaml中的事件:
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
ContractActivated="Application_ContractActivated"
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>在App.xaml.cs中:
private async void Application_ContractActivated(object sender, Windows.ApplicationModel.Activation.IActivatedEventArgs e)
{
var args = e as FileSavePickerContinuationEventArgs ;
if (args != null)
{
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);
}
}
}https://stackoverflow.com/questions/24048626
复制相似问题