Vous pouvez résoudre ce problème en ajoutant plusieurs contrôles RichTextBox à l'intérieur d'une pile ou d'un défilement. Vous devez calculer la taille de RichTextBox tout en ajoutant chaque bloc de texte. Lorsque jamais la taille semble dépasser 2048 pixels en hauteur/largeur, vous devez ajouter le texte dans le nouveau TextBlack riche.
Trouvez l'exemple de code ci-dessous pour TextBlock implémenté de la même manière.
Etape 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>
Etape 2:
invoquer simplement la méthode ProcessTextLength() pendant le chargement de la page.
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))
});
}
}
Ceci résoudra votre problème. S'il vous plaît marquer comme réponse si cela vous aide vraiment.
Merci Kamal.