【发布时间】:2014-01-24 01:16:26
【问题描述】:
我正在尝试为已绘制到表单中的 2d 对象创建保存/加载功能。
type circle = { X : int; Y : int; Diameter : int; Brush : Brush}
type Square = { X : int; Y : int; Length : int; Height: int; Brush : Brush}
当我创建对象时,我将它们放入 2 个列表中,每种类型 1 个。 我最初的想法是读取这些对象并将其写入文本文件,见下文:
saveFile.Click.Add(fun _ ->
for c in listOfCircles do
myfile.WriteLine("Circle," + c.X.ToString() + "," + c.Y.ToString() + "," + c.Diameter.ToString() + "," + c.Brush.ToString())
for s in listOfSquares do
myfile.WriteLine("Square," + s.X.ToString() + "," + s.Y.ToString() + "," + s.Height.ToString() + "," + s.Length.ToString() + "," + s.Brush.ToString())
myfile.Close() // close the file
在文本文件中它看起来像这样
Circle,200,200,50,System.Drawing.SolidBrush
Square,50,55,45,55,System.Drawing.SolidBrush
从这里我想读取这些值,然后能够通过将对象添加到列表中并重新绘制它们来解析它们并重新创建对象。
let readCircle =
System.IO.File.ReadAllLines path
|> Array.choose (fun s ->
match s.Split ',' with
| [|x; y ; z ; b ; _|] when x = "Circle" -> Some (y, z, b)
| _ -> None )
let readSquare =
System.IO.File.ReadAllLines path
|> Array.choose (fun s ->
match s.Split ',' with
| [|x; y ; z ; b ; a ; _|] when x = "Square" -> Some (y, z, b, a)
| _ -> None )
这些功能给了我
val readCircle : (string * string * string) [] = [|("200", "200", "50")|]
val readSquare : (string * string * string * string) [] = [|("50", "55", "45", "55")|]
我现在遇到的问题是我不确定如何从数组中获取值。下面是多个圆圈的示例。
val readCircle : (string * string * string) [] = [|("200", "200", "50"); ("200", "200","50")|]
非常感谢有关如何从这里开始/如何解决此问题的任何想法或 cmets!问题摘要:我如何从数组中获取值并将它们放入例如我已经创建的添加函数,见下文:
listOfCircles.Add({ X = 200; Y = 200; Diameter = 50; Brush = Brushes.Black})
【问题讨论】:
-
我不确定我是否理解这个问题。但假设您需要将值数组转换为形状列表,您可以使用
values |> Seq.map createCircle |> Seq.toList。 -
我想从数组中获取值并放入listOfCircles.Add函数,见编辑底部。
标签: f#