C# 将FileSavePicker更改为从独立存储而不是应用存储保存文件。例外情况WP8.1
用var…行更新了SoundData中的代码。实际上,我已经看到了要保存到现在的文件夹,但是当我尝试选择一个文件夹并保存到它时,我遇到了一个异常 +$exception{System.IO.FileNotFoundException:系统找不到指定的文件。 在app.xaml.cs上:C# 将FileSavePicker更改为从独立存储而不是应用存储保存文件。例外情况WP8.1,c#,windows-phone-8,isolatedstorage,filesavepicker,C#,Windows Phone 8,Isolatedstorage,Filesavepicker,用var…行更新了SoundData中的代码。实际上,我已经看到了要保存到现在的文件夹,但是当我尝试选择一个文件夹并保存到它时,我遇到了一个异常 +$exception{System.IO.FileNotFoundException:系统找不到指定的文件。 在app.xaml.cs上: private async void Application_ContractActivated(object sender, Windows.ApplicationModel.Activation.IActiv
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
var sourceFilePath = args.ContinuationData["SourceSound"].ToString();
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile sourceFile = await folder.GetFileAsync(sourceFilePath);
IRandomAccessStreamWithContentType sourceStream = await sourceFile.OpenReadAsync();
var bytes = new byte[sourceStream.Size];
var sourceBuffer = await sourceStream.ReadAsync(bytes.AsBuffer(), (uint)sourceStream.Size, Windows.Storage.Streams.InputStreamOptions.None);
await FileIO.WriteBufferAsync(file, sourceBuffer);
// 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);
}
}
}
IStorageFile sourceFile=wait folder.GetFileAsync(sourceFilePath);
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
var sourceFilePath = args.ContinuationData["SourceSound"].ToString();
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile sourceFile = await folder.GetFileAsync(sourceFilePath);
IRandomAccessStreamWithContentType sourceStream = await sourceFile.OpenReadAsync();
var bytes = new byte[sourceStream.Size];
var sourceBuffer = await sourceStream.ReadAsync(bytes.AsBuffer(), (uint)sourceStream.Size, Windows.Storage.Streams.InputStreamOptions.None);
await FileIO.WriteBufferAsync(file, sourceBuffer);
// 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);
}
}
}
SoundData视图模型:
private async void ExecuteSaveSoundAs(string soundPath)
{
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(SavePath);
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedSaveFile = file;
savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
savePicker.ContinuationData.Add("SourceSound", SavePath);
savePicker.SuggestedFileName = this.Title;
savePicker.PickSaveFileAndContinue();
}
public SoundData()
{
SaveSoundAs = new RelayCommand<string>(ExecuteSaveSoundAs);
}
以下是解决方案:
Mainpage.cs中的代码已修改:
var sourceFilePath = args.ContinuationData["SourceSound"].ToString();
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile sourceFile = await folder.GetFileAsync(sourceFilePath);
以及我的FileSavePicker代码:
private async void ExecuteSaveSoundAs(string soundPath)
{
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(SavePath);
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedSaveFile = file;
savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
savePicker.ContinuationData.Add("SourceSound", SavePath);
savePicker.SuggestedFileName = this.Title;
savePicker.PickSaveFileAndContinue();
}
private async void ExecuteSaveSoundAs(字符串声音路径)
{
var file=wait ApplicationData.Current.LocalFolder.GetFileAsync(保存路径);
FileSavePicker savePicker=新FileSavePicker();
savePicker.SuggestedSaveFile=文件;
savePicker.FileTypeChoices.Add(“MP3”,newlist(){.MP3});
savePicker.ContinuationData.Add(“SourceSound”,SavePath);
savePicker.SuggestedFileName=this.Title;
savePicker.PickSaveFileAndContinue();
}