0

所以我现在告诉 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);
        }







    }
}
4

1 回答 1

1

对于 Windows Phone,您应该遵循此操作指南文档。Silverlight 应用程序的工作流程发生了很大变化。您的应用不再像以前使用旧任务那样恢复。

您不需要遵循文档中的所有步骤,但重要的部分是覆盖OnActivatedApp.xaml.cs 中的方法。在那里,您将调用 ContinueFileSavePicker 方法。

这是一个您也可以下载的示例,它应该会有所帮助。

更新

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

// 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); 
        }
    }
}
于 2014-06-04T22:43:50.270 回答