我已经能够让它工作,但不幸的是,我认为没有一个答案可以自动适用于任何字体。我可能是错的,但这是我如何让它工作的:
我选择了一个固定宽度的字体(Consolas)和一个特定的大小。我认为大小并不重要。
选择字体大小后,我将 RichTextBlock 放入 ViewBox 并计算出设置 RichTextBlock 所需的 WIDTH 以在屏幕上获得我想要的确切字符数(或列数)。在字体大小为 13.333 时,要获得正好 60 列,我需要 440 的宽度。
在 ViewBox 内使用 13.333 字体大小和 440 的固定宽度,无论屏幕分辨率或方向如何以及文本换行,我总是能够准确地获得 60 列。既然这行得通,我需要在禁用自动换行的情况下解决您的问题。
您希望文本能够从屏幕上流出并允许用户向任一方向平移,但您始终需要固定数量的列(在我的示例中为 60)。为此,我从 RichTextBox 中删除了固定宽度,并确定了 ScrollViewer 需要设置的缩放级别才能始终获得 60 列宽。事实证明,缩放级别是当前屏幕宽度除以 440(我在上面为这个特定字体和大小想出的神奇宽度)。
所以,这里是 xaml:
<ScrollViewer x:Name="Scroller" ZoomMode="Disabled" HorizontalScrollBarVisibility="Visible" >
<RichTextBlock x:Name="TextBlock">
<Paragraph FontSize="13.333" FontFamily="Consolas">
<Run Text="…" FontFamily="Consolas"/>
</Paragraph>
</RichTextBlock>
</Viewbox>
</ScrollViewer>
对于自动换行:
TextBlock.Width=440
Scroller. ZoomToFactor(1.0f)
对于自动换行关闭:
TextBlock.Width=Auto
Scroller.ZoomToFactor((float)Window.Current.Bounds.Width / 440);
It’s important to change the zoom level whenever the window size changes. This could be because the user rotated the device, because they connected an external monitor, because you’ve gone into snapped view, etc. You can get notified whenever this happens by subscribing to the Window.Current.SizeChanged event. Inside the event, be sure to use the parameter passed to you because the window hasn’t actually changed size yet at this point. So:
void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
Scroller.ZoomToFactor((float)e.Size.Width / 440);
}
Finally, obviously the Width of 440 is tied to that particular font, font size and the fact that I always want 60 columns displayed. Once I figured it out, it should never change on any display size as long as I use the same font and font size. If you want to allow the user to pick different fonts, you’re going to have to figure out a way to calculate that dynamically. I couldn’t find any answer for this in my 20 minutes of searching. Maybe someone else can find that.