2016-12-04 65 views
1

我正在使用RichEditBox,我在保存和恢復Rtf文本時遇到了一些問題。RichEditBox(UWP)在設置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); 

而且文本在控件加載,但字體系列和前景顏色是COM完全忽略。 我已經試過電話:

EditBox.Document.ApplyDisplayUpdates(); 

它不能解決問題。我的意思是,爲什麼所有其他段落/字符選項都恢復正確,但只是忽略字體系列和前景色?我在這裏錯過了什麼嗎? 謝謝!

編輯:這是我使用的RichEditBox定製Style

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

回答

1

目前不能在最新窗口通過代碼10的構建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); 
} 

如果您仍想在您的環境中使用某種解決方法,請嘗試在RichEditTextBox的風格下找到以下「聚焦ViewState」下的代碼並註釋掉它。欲瞭解更多詳情,請參考this thread

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

而且就在我身邊,結果: enter image description here

+0

非常感謝你的回答!我已經刪除了這些視覺狀態(我用我正在使用的完整樣式編輯了我的答案),我剛剛按照您的建議(我在頁面構造函數中調用它)將SetText調用移入Loaded事件處理程序,並解決了問題。乾杯! – Sergio0694