1

I am trying to learn to write a windows phone 8.1 app using c#. I want the app to get location data, create a GPX file and write the data into a waypoint. I can get the location, write the xml stuff I need and create a new file using the FileSavePicker. The problem is the FileSavePicker will only create an empty file, but everything that is written into the ContinueFileSavePicker part will not work. Does anyone know what I'm missing? Thanks

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.
    /// This parameter is typically used to configure the page.</param>
    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        //await getLocation();

    }

    private async void Location_Click(object sender, RoutedEventArgs e)
    {

        await getLocation();

    }

    private  void Save_Click(object sender, RoutedEventArgs e)
    {
        // Clear previous returned file name, if it exists, between iterations of this scenario
        Status2.Text = "";

        FileSavePicker savePicker = new FileSavePicker();
        savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        // Dropdown of file types the user can save the file as
        savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
        // Default file name if the user does not type one in or select a file to replace
        savePicker.SuggestedFileName = "New Document";

        savePicker.PickSaveFileAndContinue();
    }

     //<summary>
     //Handle the returned file from file picker
     //This method is triggered by ContinuationManager based on ActivationKind
     //</summary>
     //<param name="args">File save picker continuation activation argment. It cantains the file user selected with file save picker </param>


    public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
    {
        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)
            {
                Status2.Text = "File " + file.Name + " was saved.";
            }
            else
            {
                Status2.Text = "File " + file.Name + " couldn't be saved.";
            }
        }
        else
        {
            Status2.Text = "Operation cancelled.";
        }
    }
Tom
  • 11
  • 1
  • please show the code where you call `ContinueFileSavePicker`. It's probably the same issue as [this one](http://stackoverflow.com/questions/24048626/filesavepicker-saving-0-bytye-file-windows-phone-8) – thumbmunkeys Mar 17 '15 at 16:20

1 Answers1

0

The answer is found here

Reads:

Writing a text file These methods use to write a new text file in the Local folder:

  • Create the new file using the method StorageFolder.CreateFileAsync.
  • Open the file using the method StorageFile.OpenAsync, Which returns a stream.
  • Open to DataWriter over the stream returned by openAsync.
  • Write the text to the stream With the DataWriter.WriteString method.
  • Flush and close the DataWriter StoreAsync using the method.

In my case the information I have in an array of bytes called "B". Then:

Await FileIO.WriteBytesAsync (File, B)

Behzad
  • 3,502
  • 4
  • 36
  • 63