Why are there self-duplicating curly braces in a saved WPF RichTextBox?

Our tester cast braces on our persistent WPF RichTextBoxes. When you save and reopen, magic more curly braces appear.

I cut the problem / code down.

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>

    <RichTextBox x:Name="rtb1" />
    <Button Grid.Row="1" Click="Button_Click">Draw a fish</Button>
    <RichTextBox x:Name="rtb2" Grid.Row="2"/>
</Grid>
</Window>

Two rich text boxes. When the button is pressed, the lower part is set to the result of the first after saving and restoring.

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            rtb1.Document = new FlowDocument(new Paragraph(new Run("{")));
        }


        public static FlowDocument CreateFlowDocumentFromByteArray(byte[] byteArray)
        {
            return (FlowDocument)XamlReader.Load(new MemoryStream(byteArray));
        }

        public static byte[] CreateByteArrayFromFlowDocument(FlowDocument flowDocument)
        {
            MemoryStream mStream = new MemoryStream();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = false;
            settings.OmitXmlDeclaration = true;
            XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(mStream, settings));
            dsm.XamlWriterMode = XamlWriterMode.Value;
            XamlWriter.Save(flowDocument, dsm);
            mStream.Close();
            return mStream.ToArray();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            rtb2.Document = CreateFlowDocumentFromByteArray(CreateByteArrayFromFlowDocument(rtb1.Document));
        }

    }
}

Why is this happening? How to stop it?

+3
source share
1 answer

I'll run the code in more detail tonight, but will this happen when the braces are not at the beginning of the text? For example, if your launch was "Hello {World}", is it still doing this?

, , , WPF, . :

<Button Content="{Hello}" />

, :

<Button Content="{}{Hello}" />

XamlReader.Load, {} XAML, . . , XAML?

+1

Source: https://habr.com/ru/post/1703789/


All Articles