richtextbox 调用 selectAll() 之后执行"Ctrl+C"抛异常

 

   问题产生过程: 在项目需求过程中,需要手动调用SelectAll 去让richtextbox的所有文字处于选中状态,这样设置之后如果在键盘上按下"Ctrl+C"执行复制.会在库里面抛出异常.

 

   异常分析:以下是call stack

> mscorlib.dll!System.Number.ParseDouble(string value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) + 0x1dd bytes
mscorlib.dll!System.Convert.ToDouble(string value, System.IFormatProvider provider) + 0x2b bytes
PresentationFramework.dll!System.Windows.Documents.Converters.StringToDouble(string s, ref double d) + 0x40 bytes
PresentationFramework.dll!System.Windows.Documents.XamlToRtfWriter.XamlParserHelper.ConvertToLineHeight(System.Windows.Documents.ConverterState converterState, string s, ref double d) + 0xb bytes
PresentationFramework.dll!System.Windows.Documents.XamlToRtfWriter.XamlIn.HandleAttributes(System.Windows.Documents.ConverterState converterState, System.Windows.Documents.IXamlAttributes attributes, System.Windows.Documents.DocumentNode documentNode, System.Windows.Documents.XamlToRtfWriter.XamlTag xamlTag, System.Windows.Documents.DocumentNodeArray dna) + 0xc9e bytes
PresentationFramework.dll!System.Windows.Documents.XamlToRtfWriter.XamlIn.System.Windows.Documents.IXamlContentHandler.StartElement(string nameSpaceUri, string localName, string qName, System.Windows.Documents.IXamlAttributes attributes) + 0xdb bytes
PresentationFramework.dll!System.Windows.Documents.XamlToRtfParser.ParseXTokStartElement(System.Windows.Documents.XamlToRtfParser.XamlToken xamlToken, ref string name) + 0x49 bytes
PresentationFramework.dll!System.Windows.Documents.XamlToRtfParser.Parse() + 0xa9 bytes
PresentationFramework.dll!System.Windows.Documents.XamlToRtfWriter.Process() + 0x14 bytes
PresentationFramework.dll!System.Windows.Documents.XamlRtfConverter.ConvertXamlToRtf(string xamlContent) + 0x54 bytes
PresentationFramework.dll!System.Windows.Documents.TextEditorCopyPaste.ConvertXamlToRtf(string xamlContent, System.IO.Stream wpfContainerMemory) + 0x4f bytes
PresentationFramework.dll!System.Windows.Documents.TextEditorCopyPaste._CreateDataObject(System.Windows.Documents.TextEditor This, bool isDragDrop) + 0x1d6 bytes
PresentationFramework.dll!System.Windows.Documents.TextEditorCopyPaste.Copy(System.Windows.Documents.TextEditor This, bool userInitiated) + 0xa1 bytes
PresentationFramework.dll!System.Windows.Documents.TextEditorCopyPaste.OnCopy(object target, System.Windows.Input.ExecutedRoutedEventArgs args) + 0x59 bytes
PresentationCore.dll!System.Windows.Input.CommandBinding.OnExecuted(object sender, System.Windows.Input.ExecutedRoutedEventArgs e) + 0x65 bytes
PresentationCore.dll!System.Windows.Input.CommandManager.ExecuteCommandBinding(object sender, System.Windows.Input.ExecutedRoutedEventArgs e, System.Windows.Input.CommandBinding commandBinding) + 0x92 bytes

根据以上stack的分析,可以看出,richTextbox在执行Copy的时候会将XAML转化成RTF,在转化的过程中会解析Docment的LineHeight参数,这个参数一般都会设置成"Auto",但是转化的时候使用StringToDouble去解析这个值,当值是"Auto"的时候就抛出异常了.

  接下来再分析richtexbox的XAML文件结构

<?xml version="1.0"?>
-<Section xml:lang="en-us" Typography.StylisticAlternates="0" Typography.ContextualSwashes="0" Typography.StandardSwashes="0" Typography.EastAsianLanguage="Normal" Typography.EastAsianWidths="Normal" Typography.NumeralAlignment="Normal" Typography.NumeralStyle="Normal" Typography.Capitals="Normal" Typography.Variants="Normal" Typography.EastAsianExpertForms="False" Typography.MathematicalGreek="False" Typography.SlashedZero="False" Typography.Fraction="Normal" Typography.StylisticSet20="False" Typography.StylisticSet19="False" Typography.StylisticSet18="False" Typography.StylisticSet17="False" Typography.StylisticSet16="False" Typography.StylisticSet15="False" Typography.StylisticSet14="False" Typography.StylisticSet13="False" Typography.StylisticSet12="False" Typography.StylisticSet11="False" Typography.StylisticSet10="False" Typography.StylisticSet9="False" Typography.StylisticSet8="False" Typography.StylisticSet7="False" Typography.StylisticSet6="False" Typography.StylisticSet5="False" Typography.StylisticSet4="False" Typography.StylisticSet3="False" Typography.StylisticSet2="False" Typography.StylisticSet1="False" Typography.CaseSensitiveForms="False" Typography.CapitalSpacing="False" Typography.Kerning="True" Typography.HistoricalForms="False" Typography.ContextualAlternates="True" Typography.AnnotationAlternates="0" Typography.HistoricalLigatures="False" Typography.DiscretionaryLigatures="False" Typography.ContextualLigatures="True" Typography.StandardLigatures="True" Foreground="#FF000000" FontSize="13" FontStretch="Normal" FontWeight="Normal" FontStyle="Normal" FontFamily="Arial" NumberSubstitution.Substitution="AsCulture" NumberSubstitution.CultureSource="User" FlowDirection="LeftToRight" IsHyphenationEnabled="False" LineHeight="Auto" TextAlignment="Left" xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">-

<List MarkerStyle="Disc">-

<ListItem>-<Paragraph>

<Run xml:lang="zh-cn">n</Run><Run xml:lang="zh-cn" Foreground="#00280050" FontWeight="Bold">oo</Run><Run xml:lang="zh-cn" Foreground="#00280050">ijf</Run><Run xml:lang="zh-cn">das</Run></Paragraph>

</ListItem>

</List>-<List MarkerStyle="Disc">-

<ListItem>-<Paragraph><Run xml:lang="zh-cn">fd</Run><Run xml:lang="zh-cn" FontSize="12">afd</Run><Run xml:lang="zh-cn" FontFamily="Arial Rounded MT">asf</Run></Paragraph></ListItem></List>-<Paragraph><Run xml:lang="zh-cn" Foreground="#00FBA281"> </Run></Paragraph></Section>

 

可以看出,lineheight是section的属性,并不是blocks的属性.

 由此得出,调用SelectAll的时候,会将整个XAML选择进去,而不仅仅是Blocks.

解决方案如下:

  richbox.SelectAll()

 替换为

 RichControl.Selection.Select(RichControl.Document.Blocks.FirstBlock.ContentStart, RichControl.Document.Blocks.LastBlock.ContentEnd);

 只将blocks放入selection.

 

 运行正常,没有问题.

转载于:https://www.cnblogs.com/vividand/articles/3966060.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值