gpt4 book ai didi

c# - 更改 FileSavePicker 以从独立存储而不是应用程序存储中保存文件。异常 WP8.1

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:56 25 4
gpt4 key购买 nike

用 var... 行更新了我的 SoundData 中的代码。我实际上已经看到要保存到现在的文件夹,但是当我尝试选择一个文件夹并保存到其中时出现异常。

+ $exception {System.IO.FileNotFoundException: 系统找不到指定的文件。

在 app.xaml.cs 上:

IStorageFile sourceFile = await 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);
}
}
}

声音数据 View 模型:

 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);
}

声音模型:

private SoundGroup CreateSoftwareGroup()
{
SoundGroup data = new SoundGroup();

data.Title = "OS/Software";

data.Items.Add(new SoundData
{
Title = "Test1",
FilePath = "https://xxxxx.test1.mp3",
Groups = "Test",
SavePath = "Test1.mp3"
});

data.Items.Add(new SoundData
{
Title = "Test2",
FilePath = "https://xxxxx.Test2.mp3",
Groups = "Test",
SavePath = "Test2.mp3"
});

最佳答案

解决方法如下:

修改了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();
}

关于c# - 更改 FileSavePicker 以从独立存储而不是应用程序存储中保存文件。异常 WP8.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24542358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com