RichEditBox (UWP) 在设置 Rtf 文本时忽略字体和前景

问题描述 投票:0回答:2

我正在使用

RichEditBox
,但在保存和恢复 Rtf 文本时遇到一些问题。

这是我导出的示例 Rtf 文本,您可以看到正确保存了不同的前景色和字体系列(我手动缩进了它以使其更易于阅读)

{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1040
    {\fonttbl{\f0\fnil\fcharset0 Segoe UI;}
    {\f1\fnil\fcharset0 Brush Script MT;}
    {\f2\fnil\fcharset0 Impact;}
    {\f3\fnil Segoe UI;}}
{\colortbl ;\red9\green105\blue192;\red0\green0\blue0;\red76\green152\blue25;\red208\green52\blue56;}
{\*\generator Riched20 10.0.14393}\viewkind4\uc1 
\pard\ltrpar\tx720\cf1\b\i\f0\fs23 Test\cf2\b0\i0\par
\pard\ltrpar\li320\qc\tx720\cf3\strike\f1\fs40 Some color\cf2\strike0\f0\fs23\par
\pard\ltrpar\tx720\cf4\f2 Hello!\cf2\f0\par
\f3\par
\f0\lang1033\par
\f3\par
\par
\pard\ltrpar\tx720\fs23\par
}

问题:我打电话

EditBox.Document.SetText(TextSetOptions.FormatRtf, myRtfString);

文本已加载到控件中,但字体系列和前景色完全被忽略。 我已经尝试过打电话:

EditBox.Document.ApplyDisplayUpdates();

这并不能解决问题。我的意思是,为什么所有其他段落/字符选项都正确恢复,但只是忽略字体系列和前景色?我在这里错过了什么吗? 谢谢!

编辑:这是自定义的

Style
我用于
RichEditBox

<Style TargetType="RichEditBox" x:Key="CustomRtfEditBoxStyle">
    <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}" />
    <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}" />
    <Setter Property="Foreground" Value="{ThemeResource TextControlForeground}" />
    <Setter Property="Background" Value="{ThemeResource TextControlBackground}" />
    <Setter Property="SelectionHighlightColor" Value="{ThemeResource TextControlSelectionHighlightColor}" />
    <Setter Property="BorderBrush" Value="{ThemeResource TextControlBorderBrush}" />
    <Setter Property="BorderThickness" Value="{ThemeResource TextControlBorderThemeThickness}" />
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
    <Setter Property="TextWrapping" Value="Wrap" />
    <Setter Property="Padding" Value="{ThemeResource TextControlThemePadding}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="RichEditBox">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Disabled" />
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="PointerOver" />
                            <VisualState x:Name="Focused" />
                            <VisualState x:Name="UnFocused" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Border x:Name="BorderElement"
                            Grid.Row="1"
                            Background="{ThemeResource TextControlBackgroundFocused}"
                            BorderThickness="0"
                            Grid.ColumnSpan="2"
                            Grid.RowSpan="1" />
                    <ContentPresenter x:Name="HeaderContentPresenter"
                                    x:DeferLoadStrategy="Lazy"
                                    Visibility="Collapsed"
                                    Grid.Row="0"
                                    Margin="0,0,0,8"
                                    Grid.ColumnSpan="2"
                                    Content="{TemplateBinding Header}"
                                    ContentTemplate="{TemplateBinding HeaderTemplate}"
                                    FontWeight="Normal" />
                    <ScrollViewer x:Name="ContentElement"
                                  RequestedTheme="Light"
                                Grid.Row="1"
                                HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                                HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                                VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                                IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                                IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                                Margin="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}"
                                IsTabStop="False"
                                ZoomMode="Disabled"
                                AutomationProperties.AccessibilityView="Raw" />
                    <ContentControl x:Name="PlaceholderTextContentPresenter"
                                    Grid.Row="1"
                                    Margin="{TemplateBinding BorderThickness}"
                                    Padding="{TemplateBinding Padding}"
                                    IsTabStop="False"
                                    Grid.ColumnSpan="2"
                                    Content="{TemplateBinding PlaceholderText}"
                                    IsHitTestVisible="False" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
c# xaml windows-runtime uwp windows-10
2个回答
1
投票

此问题目前无法在最新的 Windows 10 build 14393 中重现,代码如下:

XAML 代码

... mc:Ignorable="d" Loaded="Page_Loaded">
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
   <RichEditBox x:Name="EditBox"  Height="400" Margin="40"   >
   </RichEditBox> 
</StackPanel>

背后代码:

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    string myRtfString = @"{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1040
 {\fonttbl{\f0\fnil\fcharset0 Segoe UI;}
 {\f1\fnil\fcharset0 Brush Script MT;}
 {\f2\fnil\fcharset0 Impact;}
 {\f3\fnil Segoe UI;}}
 {\colortbl ;\red9\green105\blue192;\red0\green0\blue0;\red76\green152\blue25;\red208\green52\blue56;}
 {\*\generator Riched20 10.0.14393}\viewkind4\uc1 
 \pard\ltrpar\tx720\cf1\b\i\f0\fs23 Test\cf2\b0\i0\par
 \pard\ltrpar\li320\qc\tx720\cf3\strike\f1\fs40 Some color\cf2\strike0\f0\fs23\par
 \pard\ltrpar\tx720\cf4\f2 Hello!\cf2\f0\par
 \f3\par
 \f0\lang1033\par   
 \f3\par
 \par
 \pard\ltrpar\tx720\fs23\par
 }";
    EditBox.Document.SetText(TextSetOptions.FormatRtf, myRtfString);
} 

如果您仍然希望在您的环境中找到解决方法,请尝试在“Focused ViewState”下以

RichEditTextBox
的样式找到以下代码并将其注释掉。有关此内容的更多详细信息,请参考此线程

 <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
     <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlForegroundChromeBlackHighBrush}"/>
 </ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="RequestedTheme" Storyboard.TargetName="ContentElement">
    <DiscreteObjectKeyFrame KeyTime="0" Value="Light"/>
</ObjectAnimationUsingKeyFrames>

我这边的结果是:


0
投票

UWP RichEditBox 在自己的 OnLoaded 事件中应用父 Xaml 控件的字体和颜色默认值。您可以通过查看同步 RichEditBox::TextChanging 事件处理程序中的调用堆栈来查看它。在 RichEditBox::OnLoaded 事件中再次重新应用 RTF 可以解决问题。

在上面的回答中“当前无法通过代码在最新的 Windows 10 版本 14393 中重现此问题,如下所示:”在将 Xaml 默认值传播到 RTF 的 RichEditBox::OnLoaded 之后,在 Page_Loaded 中设置了 RTF。

© www.soinside.com 2019 - 2024. All rights reserved.