C# Windows Phone 8.1 RT PickSaveFileAndContinue()方法创建一个空文件
我正在尝试使用WP 8.1 RT中的PickSaveFileAndContinue()方法将文件保存到Documents文件夹。除保存的文件为空外,一切都正常。 当我在OnActivated()方法中从以下代码返回文件时,它的大小为零 有人吗C# Windows Phone 8.1 RT PickSaveFileAndContinue()方法创建一个空文件,c#,windows-runtime,windows-phone-8.1,filesavepicker,C#,Windows Runtime,Windows Phone 8.1,Filesavepicker,我正在尝试使用WP 8.1 RT中的PickSaveFileAndContinue()方法将文件保存到Documents文件夹。除保存的文件为空外,一切都正常。 当我在OnActivated()方法中从以下代码返回文件时,它的大小为零 有人吗 var database = await FileHelper.GetFileAsync(ApplicationData.Current.LocalFolder, DATABASE_NAME); FileSavePicker savePicker =
var database = await FileHelper.GetFileAsync(ApplicationData.Current.LocalFolder, DATABASE_NAME);
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
savePicker.FileTypeChoices.Add("Database File", new List<string>() { ".db" });
savePicker.DefaultFileExtension = ".db";
savePicker.SuggestedFileName = DATABASE_NAME;
savePicker.SuggestedSaveFile = database;
这是意料之中的。PickSaveFileAndContinue不知道应用程序要保存什么。它只提供一个空的存储文件。然后,应用程序可以将它想要保存的任何内容写入文件。为什么?我正在指定要保存的存储文件。请参考编辑后的问题。
protected async override void OnActivated(IActivatedEventArgs args)
{
byte[] buffer = null;
if(args!=null)
{
if(args.Kind == ActivationKind.PickSaveFileContinuation)
{
var file = ((FileSavePickerContinuationEventArgs)args).File;//This is empty
using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
{
buffer = new byte[stream.Size];
using (DataReader reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(buffer);
}
}
if (file != null)
{
CachedFileManager.DeferUpdates(file);
await FileIO.WriteBytesAsync(file, buffer);
Windows.Storage.Provider.FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
}
}
}
base.OnActivated(args);
}