首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FIleSavePicker保存0字节文件Windows 8

FIleSavePicker保存0字节文件Windows 8
EN

Stack Overflow用户
提问于 2014-06-04 22:25:04
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
复制
    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);
        }







    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-04 22:43:50

对于Windows,您应该遵循这个如何文档化。与Silverlight应用程序相比,工作流已经发生了很大变化。你的应用程序不再像以前那样恢复旧任务了。

您不需要遵循文档中的所有步骤,但是一个重要的部分是在App.xaml.cs中遍历App.xaml.cs方法。在其中,您将调用您的ContinueFileSavePicker方法。

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

更新

如果要保存将随应用程序传送的文件,请尝试以下代码来初始化选择器

代码语言:javascript
复制
// 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的PhoneApplicationService事件。当应用程序从选择器返回时,这个事件就会被触发(在8.1 Silverlight应用程序中)。这就是您想要调用ContinueFileSavePicker方法的地方。如果你愿意的话,你可以把逻辑放进去。

订阅xaml中的事件:

代码语言:javascript
复制
<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中:

代码语言:javascript
复制
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); 
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24048626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档