1

我正在尝试学习使用 c# 编写一个 windows phone 8.1 应用程序。我希望应用程序获取位置数据、创建 GPX 文件并将数据写入航点。我可以获取位置,编写我需要的 xml 内容并使用 FileSavePicker 创建一个新文件。问题是 FileSavePicker 只会创建一个空文件,但写入 ContinueFileSavePicker 部分的所有内容都不起作用。有谁知道我错过了什么?谢谢

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.";
        }
    }
4

1 回答 1

0

答案在这里

阅读:

写入文本文件 这些方法用于在本地文件夹中写入新的文本文件:

  • 使用方法 StorageFolder.CreateFileAsync 创建新文件。
  • 使用 StorageFile.OpenAsync 方法打开文件,该方法返回一个流。
  • 通过 openAsync 返回的流向 DataWriter 打开。
  • 使用 DataWriter.WriteString 方法将文本写入流。
  • 使用该方法刷新并关闭 DataWriter StoreAsync。

在我的情况下,我在一个名为“B”的字节数组中拥有信息。然后:

Await FileIO.WriteBytesAsync (File, B)

于 2015-07-25T03:25:24.860 回答