如何为发送到移动设备的垂直居中消息创建WYSIWYG TextBox控件编辑器
本文关键字:创建 消息 垂直居中 WYSIWYG TextBox 编辑器 控件 移动 | 更新日期: 2023-09-27 17:49:16
我有一个WPF TextBox,它包含七行文本,并且启用了换行。
<TextBox TextWrapping="Wrap" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" MaxLines="7"/>
正如您在上面的XAML中看到的那样,文本垂直和水平居中。当我输入一个适合单行的短短语时,文本出现在控件的第4行,因为VerticalContentAlignment是"Center"。
用户输入的文本将被发送到一个移动设备,该设备的显示器包含七行文本,并使用"'n"换行到下一行。其目的是使移动设备上的文本显示看起来与在TextBox控件中输入的内容相同。至少是文本的行数,居中,以及发生换行的地方。
因此,当用户在TextBox控件中输入文本并点击"Send Message"按钮时,必须对输入的文本进行一些后处理,然后才能将其发送到移动设备。
输入到TextBox控件中的文本需要在文本换行到TextBox控件中的新行处添加换行符('n)字符。例如,在控件显示多行文本的情况下,我复制TextBox的文本,并在TextBox控件换行用户输入的文本行之间添加一个换行符。
所以当用户点击"Send Message"按钮时这是进行后处理的代码:
public static String AddNewLineCharsToMessage(TextBox textBox)
{
String message = String.Empty;
if (textBox == null) return message;
// First strip all the carriage returns and newline characters
// so we don't have duplicate newline characters in the message.
// Then add back in just the newline characters which is what the
// mobile device uses to wrap lines.
// Just assign the text if we have a single line message
if (textBox.LineCount < 2)
return textBox.Text;
var textLines = new List<string>(5);
int lineCount = Math.Min(textBox.LineCount, textBox.MaxLines);
for (Int32 index = 0; index < lineCount; index++)
{
if (textBox.GetLineText(index).Length > 0)
{
textLines.Add(textBox.GetLineText(index));
textLines[index] = textLines[index].Replace("'r", "");
textLines[index] = textLines[index].Replace("'n", "");
}
else
textLines.Add("'n");
}
message = String.Empty;
for (Int32 index = 0; index < lineCount; index++)
message += textLines[index] + (index < lineCount - 1 ? "'n" : "");
return message;
}
给出上面的代码,我希望单行文本的输出看起来像:"'n'n'n'nFoo"。然而,输出是"'nFoo'nFoo'nFoo'nFoo 'nFoo'nFoo"。在代码中设置一个断点,我看到索引0到3的TextBox . getlinetext (index)为每个索引返回"Foo",即使"Foo"只在TextBox控件中显示一次。
所以我想我真的有两个问题:
1)为什么GetLineText返回LineCount为4,每行都有相同的文本,当只有一行文本(适合在TextBox控件的一行)被用户输入?
2)什么是一个简单的方法来解决这个问题,保持输入的文本在文本框控件的中心,并向远程设备发送文本消息,将显示为用户在文本框控件上看到的?
指出:我不能简单地删除重复的文本行并将其替换为"'n",因为用户可能在多行中输入了相同的文本。此外,我可以简单地将输入的文本对齐到垂直顶部,而不是垂直中心。我已经验证了这一工作,但并没有给出一个真正的所见即所得的经验。
看起来方法中有bug。您可以通过使用另一个垂直居中的控件包裹文本框或通过text属性提取行来解决这个问题。
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<TextBox AcceptsReturn="True" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" BorderThickness="0"> </TextBox>
</StackPanel>
上面所示方法的以下修改代码解决了这个问题。我仍然很好奇,如果微软有一个错误与文本框。GetLineText方法。
public static String AddNewLineCharsToMessage(TextBox textBox)
{
String message = String.Empty;
if (textBox == null) return message;
// Just assign the text if we have a single line message
if (textBox.LineCount < 2)
return textBox.Text;
// Find the index for the first line that contains text displayed in the TextBox.
// GetLineText(index) will return the text displayed/entered by the user for indices less
// than the index of the line that the text is actually displayed on. This seems to be
// a bug to me, but I will workaround this Microsoft weirdness.
// Find the index of first line that actually displays text by using the length of TextBox.Text
Int32 firstTextLineIndex = 0;
Int32 textLen = textBox.Text.Length;
Int32 textLinesLen = 0;
for (Int32 firstTextLine = textBox.LineCount - 1; firstTextLine >= 0; firstTextLine--)
{
textLinesLen += textBox.GetLineText(firstTextLine).Length;
if (textLinesLen >= textLen)
{
firstTextLineIndex = firstTextLine;
break;
}
}
// First strip all the carriage returns and newline characters
// so we don't have duplicate newline characters in the message.
// Then add back in just the newline characters which is what the car
// code uses to parse out the message to be displayed on each line.
var textLines = new List<string>(5);
int lineCount = Math.Min(textBox.LineCount, textBox.MaxLines);
for (Int32 index = 0; index < lineCount; index++)
{
if (index < firstTextLineIndex)
textLines.Add("");
else // if (textBox.GetLineText(index).Length > 0)
{
textLines.Add(textBox.GetLineText(index));
textLines[index] = textLines[index].Replace("'r", "");
textLines[index] = textLines[index].Replace("'n", "");
}
}
message = String.Empty;
for (Int32 index = 0; index < lineCount; index++)
message += textLines[index] + (index < lineCount - 1 ? "'n" : "");
return message;
}