2017-06-03 41 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

相关问题