UWP RichEditBox 的段落标记未调整大小

Paragraph mark of UWP RichEditBox is not resized

使用此代码 (Editor.Document.Selection.CharacterFormat.Size = 20;),我在 Page_Loaded 句柄中更改 UWP RichEditBox 的字体大小。当我开始输入一些字符时,一切正常。但是当我 select 段落标记然后键入内容时,此文本显示为错误的字体大小 (10.5)。

我试过在设置字号前把selection展开,但是好像没有文字的时候没有段落标记。但是,当丰富的编辑框为空并且我按下 Shift+Right Arrow ⟶(就像我通常 select 段落标记一样)时,字体大小设置回 10.5。

是否有任何解决方法来防止字体大小在任何情况下都设置回 10.5?

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBlock x:Name="FontSizeBlock" Grid.Row="0"></TextBlock>
    <RichEditBox x:Name="Editor" Grid.Row="1" SelectionChanged="HandleRichEditBox_SelectionChanged"></RichEditBox>
</Grid>


public sealed partial class MainPage : Page {
    public MainPage() {
        this.InitializeComponent();
        this.Loaded += Page_Loaded;
    }

    private void Page_Loaded(object sender, RoutedEventArgs e) {
        Editor.Document.Selection.SetRange(0, 1);
        Editor.Document.Selection.CharacterFormat.Size = 20;
    }

    private void HandleRichEditBox_SelectionChanged(object sender, RoutedEventArgs e) {
        FontSizeBlock.Text = "FontSize: " + Editor.Document.Selection.CharacterFormat.Size;
    }
}

Is there any workaround to prevent that the font size is set back to 10.5 in any case?

根据您的要求,您可以在按 Shift+Right 时将字体大小强制设置为 20。请参考以下代码。

private void HandleRichEditBox_SelectionChanged(object sender, RoutedEventArgs e)
{
    FontSizeBlock.Text = "FontSize: " + Editor.Document.Selection.CharacterFormat.Size;
    if (Editor.Document.Selection.CharacterFormat.Size == 10.5)
    {
        Editor.Document.Selection.SetRange(0, 1);
        Editor.Document.Selection.CharacterFormat.Size = 20;
    }
}

在进一步调查我的问题时,我注意到当 RichEditBox 中已经有一些文本被删除时,我的问题就不会发生。

因此,我尝试以编程方式追加、select 和删除 Page_Loaded 句柄中的字符,这种方法对我有用。

private void Page_Loaded(object sender, RoutedEventArgs e) {
    // set any character
    Editor.Document.SetText(Windows.UI.Text.TextSetOptions.None, "a");
    Editor.Document.Selection.Expand(Windows.UI.Text.TextRangeUnit.Paragraph);
    Editor.Document.Selection.CharacterFormat.Size = 20;
    Editor.Document.SetText(Windows.UI.Text.TextSetOptions.None, "");

}