locked
Loading localized text inside RichEditBox RRS feed

  • Question

  • Hi,

    a have a rich text file for every language my app supports and i want to load its content into a page, here is what i have so far:

    XAML:

    <RichEditBox x:Name="rbProText" Width="550" Height="380" BorderThickness="0" Background="Transparent" 
    ></RichEditBox>

    C#

    StorageFolder folder = await Package.Current.InstalledLocation.GetFolderAsync("assets");
                string file = string.Format("Pro Benefits - {0}.rtf", 
                    !string.IsNullOrEmpty(ApplicationLanguages.PrimaryLanguageOverride) ? ApplicationLanguages.PrimaryLanguageOverride : "pt");
                var storageFile = await folder.GetFileAsync(file);
                var stream = await storageFile.OpenReadAsync();
                rbProText.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, stream);

    My problems are:

    • The text from the file is formatted ok, as my bulleting is concerned, but it wont pick up the color i set in wordpad (i need it to be White, since my screen is dark)
    • I cant change the color of the text, after its loaded into the control (i tryied rbProText.Document.Selection.CharacterFormat.ForegroundColor = Colors.White;)
    • Is this the best way to go to have a good amount of formatted text localized in my App?

    TKS!


    Thursday, October 10, 2013 11:28 PM

Answers

  • Hi BHC Tecnologia,

    For your second question, you apply the write color for the selection text foreground, but did you select something? You could use following code to select the text:

                rbProText.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, stream);
                rbProText.Document.Selection.StartPosition = 0;
                rbProText.Document.Selection.EndPosition = 20;
                rbProText.Document.Selection.CharacterFormat.ForegroundColor = Colors.White;

    Ok, let's back to your first question. When I tried the code by a button click event, it works fine, but if we directly put the code after page initialized, the text seems to be black, all the format has been removed. I cannot explain why this happens.

    Try it and to see if it works.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, October 11, 2013 8:28 AM
    Moderator

All replies

  • Hi BHC Tecnologia,

    For your second question, you apply the write color for the selection text foreground, but did you select something? You could use following code to select the text:

                rbProText.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, stream);
                rbProText.Document.Selection.StartPosition = 0;
                rbProText.Document.Selection.EndPosition = 20;
                rbProText.Document.Selection.CharacterFormat.ForegroundColor = Colors.White;

    Ok, let's back to your first question. When I tried the code by a button click event, it works fine, but if we directly put the code after page initialized, the text seems to be black, all the format has been removed. I cannot explain why this happens.

    Try it and to see if it works.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, October 11, 2013 8:28 AM
    Moderator
  • Thank you James for the help, i have it working now and my final code is:

     public MainPage()
            {
                this.InitializeComponent();
    ...
    string text = LocalizedNamesLibrary.GetText("ProText");
                rbProText.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, text);
                textLenght = text.Length;
    ...
    }

    private void rbProText_Loaded(object sender, RoutedEventArgs e)
            {
                rbProText.Document.Selection.StartPosition = 0;
                rbProText.Document.Selection.EndPosition = textLenght - 1;
                rbProText.Document.Selection.CharacterFormat.ForegroundColor = Colors.White;
                rbProText.IsReadOnly = true;
            }

    As for my localized text, i ended up puting the rtf formatted text in my resource file even tough it's kind of big...

    Friday, October 11, 2013 10:39 AM