xaml - F# 中序列和列表之间的奇怪差异

标签 xaml f#

我在 F# 中的列表中遇到了奇怪的问题。 我正在使用 XamlXmlReader 生成列表和序列,例如:

let xaml_reader = XamlXmlReader(filename)
let nodes_seqs = seq { while xaml_reader.Read() do yield xaml_reader }

这段代码工作得很好,但如果我对列表做同样的事情:

let xaml_reader2 = XamlXmlReader(filename)
let nodes_list = [ while xaml_reader2.Read() do yield xaml_reader2 ]

结果列表不正确,即序列的头元素包含正确的 NodeType、LinePosition、LineNumber 等,但列表的头元素是一种垃圾,指向 XAML 中的另一个位置。示例:

let head_of_seq = Seq.head nodes_seq
let head_of_list = List.head nodes_list

之后我:

  • head_of_seq.NodeType = 命名空间声明
  • head_of_seq.LineNumber = 2
  • head_of_seq.LinePosition = 2

但是:

  • head_of_list.NodeType = 无
  • head_of_list.LineNumber = 111
  • head_of_list.LinePosition = 30

XAML 文件本身是正确的,由 Microsoft Bend 生成。有什么线索吗?我将不胜感激任何帮助。

最佳答案

为什么要从序列或列表中生成 XamlXmlReader 本身?也就是说,您不应该使用 yield xaml_reader.Value 来代替吗?

无论哪种情况,您最终都会得到一个多次包含相同 XamlXmlReader 实例的序列(或列表)。当您对序列进行一次迭代时,它看起来是正确的,因为读取器在读取每个元素时都会前进一次。然而,即使你的序列几乎肯定不会按照你的想法进行。例如,如果您尝试这样做会发生什么?

let first = Seq.head nodes_seqs
let first2 = Seq.head nodes_seqs

由于每次访问序列时 XamlXmlReader 都会前进,因此 first2 将显示为指向第二个元素(并查看 firstfirst2 求值后将显示相同的内容)。

关于xaml - F# 中序列和列表之间的奇怪差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4451927/

相关文章:

c# - WPF 与 RelativeSource 和 AncestorType 的绑定(bind)

c# - 在新的 Windows 10 CalendarView 上显示/设计额外信息

f# - printf 和格式规则

Json 解析 F#

c# - F# 编译器如何知道正在调用 C# 方法以及参数需要语法元组?

c# - 如何复制包含溢出内容的 FrameworkElement?

wpf - 如何强制关闭 ContextMenu(WPF 项目)?

xaml - 如何在 XAML 中表示系统常量(如 double.MaxValue)

google-maps - Xamarin Forms Geocoder : no compilation or runtime errors, 没有结果

design-patterns - 如何处理不同类型的算法输入?