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

标签 c# windows-phone-8 isolatedstorage filesavepicker

用 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/

相关文章:

c# - 升级程序集时,迁移独立存储中文件的最佳方式是什么?

windows-phone-7 - 将图像保存到隔离存储中

c# - 从 Windows Phone UWP 的 WriteableBitmap 创建 BitmapDecoder 的异常

c# - 如何验证 Paypal 付款是否真实 C#

c# - 如何选择照片选择器?

windows-phone-8 - 将 WriteableBitmap 保存为 PNG

c# - WPF:仅当其后台任务花费的时间超过指定间隔时,才显示进度对话框

c# - 如何在 C# 中将 null 数组反序列化为 null?

windows 8 手机开发可以在 windows 7 下进行吗?

c# - 不知道为什么我收到 InvalidCastException