DasElias
DasElias

Reputation: 569

C# RichEditBox - setText font family is not adopted

I have the following example, where I set the RTF text in a UWP RichEditBox with Document.SetText. My example RTF is copied from this question (which didn't help), but I have also tried it with RTF text from Document.GetText. The problem is, that the font family of the RTF text is ignored.

// Main.xaml
<Grid>
    <RichEditBox x:Name="Editor" />
</Grid>


// Main.xaml.cs
public sealed partial class MainPage : Page {
        public MainPage() {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs 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
                                     }";

            Editor.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, myRtfString);
            Editor.Document.ApplyDisplayUpdates();
        }
}

How it does look: enter image description here

How it actually should look (copied from the question linked above):

enter image description here

Upvotes: 0

Views: 152

Answers (1)

Faywang - MSFT
Faywang - MSFT

Reputation: 5868

You cannot use OnNavigatedTo for element manipulation or state change of controls on the destination page, the RichEditBox has not been loaded completely. You can subscribe a Loaded event of current page and then set the rft string in it.

public MainPage()
{
    this.InitializeComponent();

    this.Loaded += Page_Loaded;
}

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

    Editor.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, myRtfString);
    Editor.Document.ApplyDisplayUpdates();
}

Upvotes: 1

Related Questions