Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

c# - FIleSavePicker saving 0 bytye file Windows Phone 8

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







    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

Update

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

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.

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>

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...