【问题标题】:FIleSavePicker saving 0 bytye file Windows Phone 8FIleSavePicker 保存 0 字节文件 Windows Phone 8
【发布时间】:2014-06-04 22:25:04
【问题描述】:

所以我现在告诉 FileSavePicker 只创建一个空白文件,并且我必须编写额外的代码才能真正写入文件。我在 FileSavePicker 之后启动了一个任务 WriteToFile,但我不确定如何完成它。使用 FileSavePicker,用户可以选择他们希望将文件保存到的文件夹。在 WriteToFile 代码中我应该在哪里指出它,以及我究竟如何将文件源放入其中?要保存的文件都与应用程序打包在一起。我在这里以 x.mp3 为例。

    public class SoundData : ViewModelBase
    {
        public string Title { get; set; }
        public string FilePath { get; set; }



        public RelayCommand<string> SaveSoundAs { get; set; }

        private async void ExecuteSaveSoundAs(string soundPath)
        {

        string path = @"appdata:/x.mp3";
        StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        StorageFile file = await folder.GetFileAsync(path);



                {
                    FileSavePicker savePicker = new FileSavePicker();
                    savePicker.SuggestedSaveFile = file;
                    savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
                    savePicker.ContinuationData.Add("SourceSound", soundPath);
                    savePicker.SuggestedFileName = this.Title;
                    savePicker.PickSaveFileAndContinue();

                }

        }

        public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
        {
            string soundPath = (string)args.ContinuationData["SourceSound"];
            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



                await FileIO.WriteTextAsync(file, file.Name);
                // 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);
                if (status == FileUpdateStatus.Complete) ;

            }
        }



        public SoundData()
        {
            SaveSoundAs = new RelayCommand<string>(ExecuteSaveSoundAs);
        }







    }
}

【问题讨论】:

  • 你在哪里打电话ContinueFileSavePicker
  • 在 savePicker.PickSaveFileAndContinue 之后....
  • public async void ContinueFileSavePicker ......但我一定做错了。
  • 你关注How to doc了吗?
  • public async void ContinueFileSavePicker 只是一个方法声明。除非在代码中的其他地方调用该方法,否则该方法不会执行。恐怕我对 FileSavePicker 不够熟悉,无法提供更详细的帮助。

标签: c# windows-phone-8 filesavepicker


【解决方案1】:

对于 Windows Phone,您应该关注此How to documentation。 Silverlight 应用程序的工作流程发生了很大变化。您的应用不再像以前使用旧任务那样恢复。

您不需要按照文档中的所有步骤进行操作,但重要的是要完成 App.xaml.cs 中的 OnActivated 方法。在那里你将调用你的 ContinueFileSavePicker 方法。

这里是a sample,您也可以下载它,这应该会有所帮助。

更新

如果您想保存将与您的应用一起发布的文件,请尝试以下代码来初始化选取器

// Get the local file that is shipped with the app
// file but be "content" and not "resource"
string path = @"Assets\Audio\Sound.mp3";
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await folder.GetFileAsync(path);

// Show the picker
FileSavePicker savePicker = new FileSavePicker();
// Set the file that will be saved
savePicker.SuggestedSaveFile = file;
savePicker.SuggestedFileName = "Sound";
savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
savePicker.PickSaveFileAndContinue();

您还需要确保侦听 PhoneApplicationService 的 ContractActivated 事件。当应用程序从选取器返回时,将触发此事件(在 8.1 Silverlight 应用程序中)。这是您要调用 ContinueFileSavePicker 方法的地方。如果你愿意,你总是可以把逻辑放在那里。

在 xaml 中订阅事件:

<Application.ApplicationLifetimeObjects>
    <!--Required object that handles lifetime events for the application-->
    <shell:PhoneApplicationService
        ContractActivated="Application_ContractActivated"
        Launching="Application_Launching" Closing="Application_Closing"
        Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>

并且在 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 
            await FileIO.WriteTextAsync(file, file.Name); 
            // 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); 
        }
    }
}

【讨论】:

  • 谢谢。我确实看过它,我对样本的唯一问题是其中没有要保存的实际文件。由于我对这方面的很多东西都很陌生,所以现实世界的例子最有帮助。我可以看到它是如何工作的,但由于他们没有要保存的示例文件,我看不出如何正确地做源代码。
  • 该示例确实包含使用 SaveFilePicker 的代码。看看:code.msdn.microsoft.com/windowsapps/File-picker-sample-9f294cba/…
  • 是的。但它似乎实际上并不包含要保存的文件。有点只是通过议案。我离开了 WP 场景 4,希望他们在项目中包含一个实际文件,这样我就可以看到如何将它放入要编写的代码中。例如,如果我在“appdata:/x.mp3”处有一个 mp3 文件,我无法根据示例确定它在代码中的位置。
  • 那么,您正在尝试获取与应用程序一起打包的文件?
  • 是的。把它们加起来。我的应用程序有按钮,按一次,它们会播放声音。按住会调出带有“保存到”选项的上下文菜单。现在,它保存了一个文件,但它是空白的。声音文件与应用程序打包在一起。我以前从未编写过任何代码,所以我不确定如何将其合并到上面的代码中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多