【发布时间】:2015-12-22 09:27:21
【问题描述】:
我正在开发 Windows Phone 8.1 应用程序,但遇到了这个问题:
我有一个 RichTextBlock 控件来保存我的文本,如果我的控件高度大于我的屏幕,我需要在用户阅读文本时自动逐行滚动。
有什么方法可以确定我的RichTextBlock 中的行数或者几何计算是唯一的方法吗?
我尝试遍历 Blocks 集合,但似乎没有任何相关性。
我唯一附带的就是使用TextPointer.GetCharacterRect 函数:
if(msgContainer.Blocks.Any())
{
var item = msgContainer.Blocks.FirstOrDefault();
var height = item.LineHeight;
var startRect = item.ContentStart.GetCharacterRect(LogicalDirection.Forward);
var lineHeight = startRect.Height;
var lineCount = (int)_fatAssTextMessage.ActualHeight / lineHeight;
}
但它并不准确 - 有时它会丢失一两行,因为 int 强制转换和除法...
任何帮助将不胜感激
【问题讨论】:
-
richTextBox.Lines.Count();? -
好吧,我的控件中没有属性 Lines:Windows.UI.Xaml.Controls.RichTextBlock
-
然后在全文中计算
'\r'和'\n'。它们等于 ENTER 键或换行符。 -
@FiN 文本框可以换行,使用换行符并不总是有效。
标签: c# xaml windows-phone-8.1 richtextblock