FileSavePicker for Audio文件在Windows Phone 8.1 Universal Apps中不起作用

时间:2015-07-16 04:47:37

标签: c# windows-phone-8.1 win-universal-app

我在下面的代码中尝试了这个,音频文件正在保存,但它显示0个字节,如果有人知道这个,我试了很多,请帮帮我...

WP8.1通用应用

Mainpage.cs中的代码:

private async void pick_Click(object sender, RoutedEventArgs e)
        {
            string path = @"Assets\Audio\DeviPrasad.mp3";
            StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFile file = await folder.GetFileAsync(path);

            var st =await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

            var size = st.Size;

            FileSavePicker savePicker = new FileSavePicker();
            //savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            savePicker.SuggestedSaveFile = file;
            savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
            savePicker.ContinuationData.Add("SourceSound", path);
            savePicker.SuggestedFileName = "DeviPrasad";

            savePicker.PickSaveFileAndContinue();            
        }

internal async void ContinueFileOpenPicker(FileSavePickerContinuationEventArgs e)
        {
            var file = e.File;
            var ff= file.Properties;
            if(file!=null)
            {
                CachedFileManager.DeferUpdates(file);                  
                await FileIO.WriteTextAsync(file, file.Name);                    
                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
            }
        }

App.xaml.cs中的代码:

protected async override void OnActivated(IActivatedEventArgs args)
        {
            var root = Window.Current.Content as Frame;
            var mainPage = root.Content as MainPage;
            if (mainPage != null && args is FileSavePickerContinuationEventArgs)
            {
                mainPage.ContinueFileOpenPicker(args as FileSavePickerContinuationEventArgs);
            }
        }

1 个答案:

答案 0 :(得分:0)

FileSavePicker无法帮助您将文件内容写入或复制到目的地。

实际上,我认为你只是从某个地方复制以下代码,但不知道它在做什么。

await FileIO.WriteTextAsync(file, file.Name);  

这是写东西到文件,似乎是来自处理txt文件的样本。对于您的情况,我们需要做以下事情:

  1. 在pick_Click事件中将源文件路径添加到ContinuationData。将代码更改为: savePicker.ContinuationData.Add("SourceSound", file.Path);

  2. 在ContinueFileOpenPicker中读取源文件路径和目标文件,以编写内容如下:

        var file = e.File;
        string soundPath = (string)e.ContinuationData["SourceSound"];
        //var ff = file.Properties;
    
        if (file != null)
        {
            CachedFileManager.DeferUpdates(file);
    
            StorageFile srcFile = await StorageFile.GetFileFromPathAsync(soundPath);
    
            await srcFile.CopyAndReplaceAsync(file);
    
            FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
        }