FILESavePicker 保存 0 个字节文件 Windows Phone 8 [英] FIleSavePicker saving 0 bytye file Windows Phone 8

查看:17
本文介绍了FILESavePicker 保存 0 个字节文件 Windows Phone 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So Im now told that the FileSavePicker only creates a blank file, and that Ill have to write additional code to then actually write to the file. Ive started a Task WriteToFile after the FileSavePicker but Im unsure how to finish it. With the FileSavePicker, the user selects the folder they wish to save the file to. Where do I point to that in the WriteToFile code and how exactly do I put the file source in it? The files to be saved are all packaged with the app. Im using x.mp3 as an example here.

    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);
        }







    }
}

推荐答案

对于 Windows Phone,你应该遵循这个 如何记录.Silverlight 应用程序的工作流程发生了很大变化.您的应用程序不再像以前使用旧任务那样恢复.

For Windows Phone you should follow this How to documentation. The workflow has changed quite a bit from Silverlight apps. Your app no longer resumes like it used to with the old Tasks.

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

You don't need to follow all of the steps in the doc, but an important piece is to over the OnActivated method within App.xaml.cs. Within there you will call your ContinueFileSavePicker method.

这里是示例,您也可以下载这应该会有所帮助.

Here is a sample that you can download as well that should help.

更新

如果您想保存将随应用一起提供的文件,请尝试使用以下代码来初始化选择器

If you want to save a file that you will be shipping with your app, try the following code to initialize the picker

// Get the local file that is shipped with the app
// file but be "content" and not "resource"
string path = @"AssetsAudioSound.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 方法的地方.如果你愿意,你总是可以把逻辑放在那里.

You also need to make sure that you listen to the ContractActivated event of the PhoneApplicationService. This event is fired (in 8.1 Silverlight apps) when the app returns from the picker. This is where you want to call your ContinueFileSavePicker method. If you wanted, you could always just put the logic in there.

在 xaml 中订阅事件:

Subscribe to the event in 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 中:

And in the 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); 
        }
    }
}

这篇关于FILESavePicker 保存 0 个字节文件 Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆