简体   繁体   中英

Trouble writing text from a RichEditBox to a file with a C# Windows Store app

I'm a beginner to Windows 8 development in C# and XAML. I'm trying to make a basic text editor, but I'm having trouble making the save function work. My code is:

private async void SaveAs_Click(object sender, RoutedEventArgs e)
{
    FileSavePicker savePicker = new FileSavePicker();
    savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
    savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
    savePicker.FileTypeChoices.Add("Markdown", new List<string>() { ".md" });
    savePicker.SuggestedFileName = "New Document";
    StorageFile file = await savePicker.PickSaveFileAsync();
    if (file != null)
    {
        CachedFileManager.DeferUpdates(file);
        string textboxtext = "";
        await FileIO.WriteTextAsync(file, Editor.Document.GetText(Windows.UI.Text.TextGetOptions.None, out textboxtext));
        FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
        if (status == FileUpdateStatus.Complete)
        {
        }
        else
        {
        }
    }
    else
    {
    }
}

The editor refers to a RichEditBox.

But I get two error messages:

  • "The best overloaded method match for 'Windows.Storage.FileIO.WriteTextAsync(Windows.Storage.IStorageFile, string)' has some invalid arguments"
  • "Argument 2: cannot convert from 'void' to 'string'"

Both are on the line that includes await FileIO.WriteTextAsync(file, Editor.Document.GetText(Windows.UI.Text.TextGetOptions.None, out textboxtext));

How can I fix this?

Thanks for your help.

It seems the Editor.Document.GetText Method doesnt return a value (or just a void respectively). It puts its output to the textboxtext variable instead and this variable can be used as a parameter for FileIO.WriteTextAsync method:

string textboxtext;
Editor.Document.GetText(Windows.UI.Text.TextGetOptions.None, out textboxtext)
await FileIO.WriteTextAsync(file, textboxtext);

Checkout GetText details here .

Problem : GetText returns void instead you are expecting string .

You have to do something like that:

string textboxtext = string.Empty;
Editor.Document.GetText(Windows.UI.Text.TextGetOptions.None, out textboxtext)
await FileIO.WriteTextAsync(file, textboxtext);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM