为什么持久化的 WPF RichTextBox 中有自我复制的大括号?
我们的测试人员向我们持久的 WPF RichTextBox 扔了大括号。 保存并重新打开时,会神奇地出现更多的大括号。
我已经浓缩了问题/代码。
<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>
两个富文本框。 单击按钮时,底部的结果将设置为持久和恢复后第一个结果的结果。
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));
}
}
}
为什么会发生这种情况? 我们如何阻止它?
Our tester threw curly brackets at our persisting WPF RichTextBoxes. On save and reopen, there are magically more curly brackets.
I've condensed the issue / 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. On button click, the bottom one gets set to the result of the first one after persist and restore.
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 do we stop it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
今晚我将更彻底地测试代码,但是当大括号不在文本开头时会发生这种情况吗? 例如,如果您的运行是“Hello {World}”,它还会执行此操作吗?
您可能知道,花括号在 WPF 中很重要,因为它们用于标记扩展。 以下标记不起作用:
为了获得正确的输出,您通常会使用以下方法对其进行转义:
由于您使用的是 XamlReader.Load,因此在 XAML 中使用 {} 可能会出现一些混乱,因此它们正在被逃脱。 但这只是一个猜测。 出于兴趣,写出的 XAML 是什么样子的?
I'll test the code more thoroughly tonight, but does it happen when the braces are not at the start of the text? E.g., if your run was "Hello{World}" does it still do this?
As you probably know, curly braces are significant in WPF because they are used for markup extensions. The following markup wouldn't work:
To get the right output, you'd usually escape it with:
Since you're using XamlReader.Load, there may be some confusion around the use of {}'s in the XAML, so they are being escaped. But that's just a guess. Out of interest, what does the XAML which is written out look like?