您可以通過在堆棧面板或滾動查看器中添加多個RichTextBox控件來解決此問題。您需要在添加每個文本塊的同時計算RichTextBox的大小。如果尺寸似乎超過2048像素的高度/寬度,則需要在新的Rich Textblock中添加文本。
查找下面以相同方式實現的TextBlock示例代碼。
步驟1:
<pre>
<ScrollViewer Margin="10,0,0,70">
<StackPanel Grid.Row="4" Margin="0,-36,12,12" x:Name="textBlockStackPanel">
<TextBlock x:Name="StorytextBlock" Margin="0,0,12,12" MaxHeight="2048" TextWrapping="Wrap" FontSize="24" TextTrimming="WordEllipsis" FontFamily="Segoe WP" d:LayoutOverrides="Width" Foreground="#FF464646" />
</StackPanel>
</ScrollViewer>
</pre>
步驟2:
只需調用ProcessTextLength()方法在加載頁。
private void ProcessTextLength(string story)
{
string storytext = story.Replace("\n\n", "\n\n^");
List storylist = storytext.Split('^').ToList();
List finalstorylist = new List();
string currenttext = "";
foreach (var item in storylist)
{
currenttext = this.StorytextBlock.Text;
this.StorytextBlock.Text = this.StorytextBlock.Text + item;
if(this.StorytextBlock.ActualHeight > 2048)
{
finalstorylist.Add(currenttext);
this.StorytextBlock.Text = item;
}
if (storylist.IndexOf(item) == storylist.Count - 1)
{
finalstorylist.Add(this.StorytextBlock.Text);
}
}
this.StorytextBlock.Text = "";
foreach (var finalitem in finalstorylist)
{
string text = finalitem;
if (text.StartsWith("\n\n"))
text = text.Substring(2);
if (text.EndsWith("\n\n"))
text = text.Remove(text.Length - 2);
this.textBlockStackPanel.Children.Add(new TextBlock
{
MaxHeight = 2048,
TextWrapping = TextWrapping.Wrap,
FontSize = 24,
TextTrimming = TextTrimming.WordEllipsis,
FontFamily = new FontFamily("Segoe WP"),
Text = text,
Foreground = new SolidColorBrush(Color.FromArgb(255,70,70,70))
});
}
}
這將解決您的問題。請標記爲答案,如果這真的幫助你。
謝謝 卡邁勒。