音频文件的 FileSavePicker 在 Windows Phone 8.1 通用应用程序中不起作用
FileSavePicker for Audio file is not working in Windows Phone 8.1 Universal Apps
我试过下面的代码,音频文件正在保存但它显示 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);
}
}
FileSavePicker 不会帮助您将文件内容写入或复制到目的地。
实际上,我认为您只是简单地从某个地方复制了以下代码,但并不完全知道它在做什么。
await FileIO.WriteTextAsync(file, file.Name);
这是向文件写入内容,似乎来自处理 txt 文件的样本。对于您的情况,我们需要做以下事情:
在pick_Click事件中将源文件路径添加到ContinuationData。将代码更改为:
savePicker.ContinuationData.Add("SourceSound", file.Path);
读取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);
}
我试过下面的代码,音频文件正在保存但它显示 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);
}
}
FileSavePicker 不会帮助您将文件内容写入或复制到目的地。
实际上,我认为您只是简单地从某个地方复制了以下代码,但并不完全知道它在做什么。
await FileIO.WriteTextAsync(file, file.Name);
这是向文件写入内容,似乎来自处理 txt 文件的样本。对于您的情况,我们需要做以下事情:
在pick_Click事件中将源文件路径添加到ContinuationData。将代码更改为:
savePicker.ContinuationData.Add("SourceSound", file.Path);
读取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); }