如何在 WPF RichTextBox 中使用 Blocks.Count<TSource>()?
How to use Blocks.Count<TSource>() in a WPF RichTextBox?
在 WPF RichTextBox
中,我需要计算类型 Paragraph
的所有块。 Intellisense 提供 Count<TSource>()
方法。
但是这样使用:
int paragraphNumber = this.Document.Blocks.Count<Paragraph>();
我收到编译错误 CS1929,指出 BlockCollection 不包含 "count" 的任何定义。
我哪里错了?
Count()
期望的类型是您的源集合项的类型。在您的情况下,这将是类型 Block
,因为您正在处理 BlockCollection
。这种类型通常可以从用法中推断出来并被省略,但您不能将其用作过滤方法。
你可以这样做:
int paragraphNumber = this.Document.Blocks.OfType<Paragraph>().Count();
或使用 lambda 表达式:
int paragraphNumber = this.Document.Blocks.Count(x => x is Paragraph);
在 WPF RichTextBox
中,我需要计算类型 Paragraph
的所有块。 Intellisense 提供 Count<TSource>()
方法。
但是这样使用:
int paragraphNumber = this.Document.Blocks.Count<Paragraph>();
我收到编译错误 CS1929,指出 BlockCollection 不包含 "count" 的任何定义。
我哪里错了?
Count()
期望的类型是您的源集合项的类型。在您的情况下,这将是类型 Block
,因为您正在处理 BlockCollection
。这种类型通常可以从用法中推断出来并被省略,但您不能将其用作过滤方法。
你可以这样做:
int paragraphNumber = this.Document.Blocks.OfType<Paragraph>().Count();
或使用 lambda 表达式:
int paragraphNumber = this.Document.Blocks.Count(x => x is Paragraph);