1

While working on a small UWP application that displays rich text and updates portions of it based on model changes, I've stumbled upon a really strange RichTextBlock bug. I'd appreciate if someone could provide an insight on this RichTextBlock peculiarity or give an idea on fixing this.

Here is a simplified reproduction code and bug's use case:

  • RichTextBlock with code-updated content works just fine until user clicks on it. After the click it displays one more change, but on the 2nd change the content becomes hidden.

  • After that, if user selects something in the now invisible text or presses Ctr+A the RichTextBlock would re-display it's content, but on the 2nd text change after that the content becomes hidden again.

  • What's curious, using the "Do Nothing" button makes content permanently appear, although clicking on the RichTextBlock once more makes it disappear again.

<Page
    x:Class="RichTextBlockTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Viewbox>
        <StackPanel Spacing="8" Margin="4">
            <RichTextBlock FontSize="24">
                <Paragraph>
                    <Run x:Name="timeRun">{ Time is Now }</Run>
                </Paragraph>
            </RichTextBlock>
            <Button HorizontalAlignment="Stretch">Do Nothing</Button>
        </StackPanel>
    </Viewbox>
</Page>
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace RichTextBlockTest
{
    public sealed partial class MainPage : Page
    {
        private DispatcherTimer timer =
            new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };

        public MainPage()
        {
            InitializeComponent();
            timer.Tick += (o, e) =>
            {
                timeRun.Text = DateTime.Now.ToString("HH:mm:ss");
            };
            timer.Start();
        }
    }
}

Besides the above, I've tried this scenario with binding, DependencyProperty, {x:Bind} and also with re-creating the Run and the Paragraph there instead of directly changing Run.Text from the code - all with the same result.

  • The project is targeting Windows 10, version 1803 (10.0; Build 17134)
  • Confirmed this bug on 2 boxes running Win 10 Pro 1803 - development and another clean test machine.
  • Note that Windows 10 Pro 1809 and a Server build 1809 don't show this bug (the project was still targeting 1803 when tested). There are some quirks how RichTextBlock displays text when user selects a portion of it while the text is modified including temporary disappearance similar to the bug above, but in 1809 a) disappearance is not permanent and requires not just a click, but a selection in progress, b) it's a gray area how the control should behave if user-selected text was modified by the application mid-flight, and c) there are easy and logical workarounds there, e.g. pause automatic text changes if user tries to select something.

Update 2018-12-11

  • VS.Net was updated to 15.9.4 that also required projects targeting 1803 downgrade(!) Microsoft.NETCore.UniversalWindowsPlatform version from 6.2.2 down to 6.1.9. The bug is still reproducible in Windows 10 Pro 1803.
DK.
  • 3,173
  • 24
  • 33
  • cannot reproduce this with version 1703 (Build 15063). So this may be a newly introduced bug. – kennyzx Dec 05 '18 at 01:38
  • By the way, `Run.Text` is not a dependency property, so I don't think you can use Data Binding to update this property. – kennyzx Dec 05 '18 at 01:40
  • Thank you for checking @kennyzx. I've rebuilt this project targeting 1703 (Build 15063) and platform 6.1.9 (1803 was using 6.2.2). Apparently the "disappearing text" is still there, so I'm not exactly sure what to make of it. Anyway, with 1809 being released I'm curious how this code will behave in it. As for the binding - yes, you can bind `Run.Text` _to_ a dependency property either with `x:Bind` or `Binding` - either way works OK for me, but guess that's kinda out of this question scope. – DK. Dec 05 '18 at 02:04
  • Oh, interesting to know it works with Data Binding, I need to do some experiments to understand this better. My understanding is a plain property does not get re-evaluated automatically when other dependencies it depends on are changed. – kennyzx Dec 05 '18 at 02:06
  • back to this question, from your description it looks like a problem with Style, that, the text does not displayed in Selected/Focused state. – kennyzx Dec 05 '18 at 02:08
  • Yes, thanks, that could be in the right direction: if I the focus is set on RichTextBlock in the code the text disappears without waiting for the user click, though still only after the 2nd update - weird. There are no styles directly assigned to the RichTextBlock. There is a compatible 'BaseRichTextBlockStyle' in the platform's generic.xaml, but that doesn't show any special handling for the focused state. Guess I'll play more with it tonight. – DK. Dec 05 '18 at 02:25
  • Ah well, I've set `Run.Foreground` to make sure more generic styles are overridden - that didn't fix it. – DK. Dec 05 '18 at 02:36
  • I created a 17134 project to test, but I cannot reproduce it. I also switch the target version to 17763 to test your code, but I still could not reproduce your issue. My OS Build is 1809. Please try to change your target version to 17763 to see if you still face this issue. – Xie Steven Dec 05 '18 at 08:30
  • @XavierXie-MSFT thanks for checking it with 1809. My main development and test boxes don't have 1809 update available yet - can't test there. I did verify though that virtual machine from Microsoft running Server 1809 and VS.Net (VS 15.8.9, NETCore 6.1.9), doesn't have this bug and updated the question to include this info... well, they've marked the VM as having VS 15.9, but it was 15.8.9; so had to test with 17134, will take your word for 17763 and maybe update the virtual Studio later to check out 17763 myself, but so far the results with 1809 look good. – DK. Dec 06 '18 at 10:01

0 Answers0