2017-06-03 43 views
0

簡單的文本編輯器UWP TextBox控件給人意想不到的效果

隨着文本框AcceptsReturn屬性檢查文本框的內容多保存到一個文本文件,結果寫在只有在記事本中打開一行的所有文本。在UWP TextBox控件中加載文件時,它打開正常。我沒有舊的WPF TextBox控件的這個問題。

我的程序有兩個按鈕,打開並保存。之間有文本框。

using System; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Popups; 
using Windows.Storage.Pickers; 
using Windows.Storage; 

// ... 
    StorageFile selectedFile; 

    private async void openFileButton_Click(object sender, RoutedEventArgs e) 
    { 
     contentsTextBox.Text = string.Empty; 
     addressText.Text = string.Empty; 
     selectedFile = null; 

     FileOpenPicker openDialog = new FileOpenPicker(); 
     openDialog.FileTypeFilter.Add("*"); 

     selectedFile = await openDialog.PickSingleFileAsync(); 
     if (selectedFile != null) 
     { 
      addressText.Text = selectedFile.Path; 

      try 
      { 
       contentsTextBox.Text = await FileIO.ReadTextAsync(selectedFile); 
      } 

      catch (ArgumentOutOfRangeException) { } // In case file is empty 
     } 
    } 

    private async void saveFileButton_Click(object sender, RoutedEventArgs e) 
    { 
     if (selectedFile != null) 
     { 
      await FileIO.WriteTextAsync(selectedFile, contentsTextBox.Text); 
      await new MessageDialog("Changes saved!").ShowAsync(); 
     } 
    } 
+0

箱子是否也設置爲多行?在Win32中,你需要兩個。 –

+0

文本框屬性窗口中沒有多行選項 – Mooncat

+0

openDialog.FileTypeFilter.Add(「*」);打破 – lindexi

回答

1

在文本框的文本屬性,端部的行是由\r表示。但是,記事本預計\r\n換行符。

有關兩種(和其他變體)差異的更多信息,請參閱see this

在將文本保存到文件之前,只需將「\ r」替換爲「\ r \ n」即可。

await FileIO.WriteTextAsync(selectedFile, contentsTextBox.Text.Replace("\r", "\r\n")); 
+0

環境換行 – lindexi

+0

@lindexi右邊。 – kennyzx

相關問題