UWP RichEditBox 的段落标记未调整大小 [英] Paragraph mark of UWP RichEditBox is not resized

查看:25
本文介绍了UWP RichEditBox 的段落标记未调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With this code (Editor.Document.Selection.CharacterFormat.Size = 20;) I change the font size of a UWP RichEditBox in the Page_Loaded-handle. When I start typing some characters, everything works fine. But when I select the paragraph mark and then type something, this text appears in the wrong font size (10.5).

我尝试的是在设置字体大小之前扩大选择范围,但是似乎没有文本时没有段落标记.但是当富编辑框是空的并且我按下 Shift+Right Arrow ⟶(我通常会选择段落标记)时,字体大小被设置回 10.5.

What I've tried is to expand the selection before setting the font size, but it seems that there is no paragraph mark when there's no text. But when the rich edit box is empty and I press Shift+Right Arrow ⟶ (as I would normally select the paragraph mark), the font size is set back to 10.5.

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

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

<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;
    }
}

推荐答案

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

When further investigating my problem, I noted that my issue doesn't occur when there has already been some text in the RichEditBox which was deleted.

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

Therefore, I have tried to programmatically append, select and remove characters in the Page_Loaded-handle, and this approach worked for me.

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, "");

}

这篇关于UWP RichEditBox 的段落标记未调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆